Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add percent sign to NSString

I want to have a percentage sign in my string after a digit. Something like this: 75%.

How can I have this done? I tried:

[NSString stringWithFormat:@"%d\%", someDigit]; 

But it didn't work for me.

like image 577
Ilya Suzdalnitski Avatar asked Apr 11 '09 07:04

Ilya Suzdalnitski


People also ask

How do you add a percent sign to a string in C++?

You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have the output appear as 10% on the screen.

How do you add a percentage sign in Java?

In Java, the modulus operator is a percent sign, %. The syntax is the same as for other operators: int quotient = 7 / 3; int remainder = 7 % 3; The first operator, integer division, yields 2.


2 Answers

The code for percent sign in NSString format is %%. This is also true for NSLog() and printf() formats.

like image 65
mouviciel Avatar answered Sep 28 '22 04:09

mouviciel


The escape code for a percent sign is "%%", so your code would look like this

[NSString stringWithFormat:@"%d%%", someDigit]; 

Also, all the other format specifiers can be found at Conceptual Strings Articles

like image 32
binarybob Avatar answered Sep 28 '22 04:09

binarybob