NSString stringWithFormat 添加百分比
如何向 stringWithFormat 函数添加百分比?例如,我想要以下内容:
float someFloat = 40.233f;
NSString *str = [NSString stringWithFormat:@"%.02f%",someFloat];
这应该导致字符串为:
40.23%
但事实并非如此。我怎样才能实现这个目标?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
%
是 printf 样式格式的字符开头,只需将其加倍即可:%
being the character beginning printf-style formats, it simply needs to be doubled:百分号的转义代码是“%%”,因此您的代码将如下所示 这
对于 NSLog() 和 printf() 格式也是如此。
引用自如何向 NSString 添加百分号。
The escape code for a percent sign is “%%”, so your code would look like this
This is also true for NSLog() and printf() formats.
Cited from How to add percent sign to NSString.