Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include a '%' in an NSString?

How can I include a percent symbol (%) in my NSString?

[NSString stringWithFormat:@"Downloading (%1.0%)",  percentage]

The above code does not include the last percent in the string.

like image 474
TomH Avatar asked Sep 16 '10 22:09

TomH


3 Answers

Carl is right about the escaping, but it alone won't work with the code you have given. You are missing a format specifier after the first percentage sign. Given percentage is a double, try:

[NSString stringWithFormat:@"Downloading (%.1f %%)",  percentage];

Note the %.1f, for formatting a double with one decimal. This gives 45.5 %. Use %.f for no decimals.

Also see http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/strings/Articles/formatSpecifiers.html

like image 65
Mads Mobæk Avatar answered Sep 22 '22 15:09

Mads Mobæk


Use %% to escape the percent sign.

like image 39
Carl Norum Avatar answered Sep 21 '22 15:09

Carl Norum


[NSString stringWithFormat:@"Downloading (%g%%)",  percentage];
like image 41
falconcreek Avatar answered Sep 22 '22 15:09

falconcreek