NSString stringWithFormat adding a percent

CodeGuy picture CodeGuy · Jan 3, 2011 · Viewed 39.7k times · Source

How can I add a percent to my stringWithFormat function? For example, I'd like to have the following:

float someFloat = 40.233f;
NSString *str = [NSString stringWithFormat:@"%.02f%",someFloat];

This should cause the string to be:

40.23%

But it is not the case. How can I achieve this?

Answer

F'x picture F'x · Jan 3, 2011

% being the character beginning printf-style formats, it simply needs to be doubled:

float someFloat = 40.233f;
NSString *str = [NSString stringWithFormat:@"%.02f%%",someFloat];