2011-11-22 39 views
0

可能重複:
NSString stringWithFormat adding a percent如何在字符串中包含百分比?

一個小白這裏的問題。

我有一個代碼:

int value = [sender value]; 
[overLabel setText:[NSString stringWithFormat:@"Overhead: %d", (int)value]]; 

這個設置標籤(舉例來說):

Overhead: 10 

我希望能夠將其設置爲:

Overhead: 10% 

如何在字符串中實現%,以便它不被用於打印值,但它被打印爲實際的%字符。

謝謝!

回答

4

就放兩%,像這樣:

[overLabel setText:[NSString stringWithFormat:@"Overhead: %d %%", (int)value]]; 
+0

謝謝!作品 – TrekOnTV2017

+0

[String Programming Guide](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265)擁有全部不同的格式說明符 –

2

您使用兩個%越獄%

int value = [sender value]; 
[overLabel setText:[NSString stringWithFormat:@"Overhead: %d%%", (int)value]]; 
+0

謝謝!作品 – TrekOnTV2017

相關問題