在NSString中包含'%'?

Tom*_*omH 10 objective-c nsstring

如何在NSString中包含百分号(%)?

[NSString stringWithFormat:@"Downloading (%1.0%)",  percentage]
Run Code Online (Sandbox Code Playgroud)

上面的代码不包括字符串中的最后一个百分比.

Mad*_*bæk 16

卡尔对于逃避是正确的,但仅凭它你所提供的代码是行不通的.在第一个百分号后面缺少格式说明符.鉴于percentage是双倍,尝试:

[NSString stringWithFormat:@"Downloading (%.1f %%)",  percentage];
Run Code Online (Sandbox Code Playgroud)

注意%.1f,用于格式化带有一个小数的double.这给了45.5 %.使用%.f了没有小数.

另请参阅http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/strings/Articles/formatSpecifiers.html


Car*_*rum 12

使用%%逃脱百分号.