views:

58

answers:

2

I have a program that calculates some user input, and I would like to show it as a percent. I just need to know how to put the '%' symbol in my code without it crashing on me.

Using,

lblUserTypedName.text = [[NSString alloc] initWithFormat: @"%2.1f", ultimate_risk];

Prints out a number, like 18.4. I would like for it to print out 18.4%. I've tried the following

lblUserTypedName.text = [[NSString alloc] initWithFormat: @"%2.1f%", ultimate_risk];

and it doesn't print the '%' symbol. I've tried the following.

lblUserTypedName.text = [[NSString alloc] initWithFormat: @"%2.1f\%\", ultimate_risk];

with no luck.

What am I missing here?

+8  A: 

Use "%%" to print a single "%". That is,

lblUserTypedName.text = [[NSString alloc] initWithFormat: @"%2.1f%%", ultimate_risk];
KennyTM
TY!!!!!!!!!!!!!!!!!
HollerTrain
+1  A: 

Search the docs in Xcode for "String format specifiers". You'll find a full list of them there.

NSResponder