How to add the % symbol directly after a variable call in Obj C?
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,
l开发者_如何学编程blUserTypedName.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?
Use "%%" to print a single "%". That is,
lblUserTypedName.text = [[NSString alloc] initWithFormat: @"%2.1f%%", ultimate_risk];
Search the docs in Xcode for "String format specifiers". You'll find a full list of them there.
精彩评论