Cod*_*Guy 37 iphone nsstring stringwithformat ios
如何在stringWithFormat函数中添加百分比?例如,我想要以下内容:
float someFloat = 40.233f;
NSString *str = [NSString stringWithFormat:@"%.02f%",someFloat];
Run Code Online (Sandbox Code Playgroud)
这应该导致字符串:
40.23%
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.我怎样才能做到这一点?
F'x*_*F'x 85
% 作为开始printf风格格式的角色,它只需要加倍:
float someFloat = 40.233f;
NSString *str = [NSString stringWithFormat:@"%.02f%%",someFloat];
Run Code Online (Sandbox Code Playgroud)
spo*_*olu 12
百分号的转义码是"%%",因此您的代码将如下所示
[NSString stringWithFormat:@"%d%%", someDigit];
Run Code Online (Sandbox Code Playgroud)
对于NSLog()和printf()格式也是如此.