我想在数字后加一个百分号。 大概是75%。 git
我该怎么作? 我试过了: spa
[NSString stringWithFormat:@"%d\%", someDigit];
但这对我没有用。 code
若是在某些状况下有帮助,则能够使用unicode字符: orm
NSLog(@"Test percentage \uFF05");
uese如下代码。 unicode
NSString *searchText = @"Bhupi" NSString *formatedSearchText = [NSString stringWithFormat:@"%%%@%%",searchText];
将输出: %Bhupi% 字符串
彷佛若是%%
后跟%@
,则NSString
将转到一些奇怪的代码,尝试此操做,这对我有用 string
NSString *str = [NSString stringWithFormat:@"%@%@%@", @"%%", [textfield text], @"%%"];
接受的答案不适用于UILocalNotification。 因为某些缘由, %%%%
(4个百分号)或Unicode字符' \%
'仅对此有效。 it
回顾一下,格式化字符串时,能够使用%%
。 可是,若是您的字符串是UILocalNotification的一部分,请使用%%%%
或\%
。 io
iOS 9.2.1,Xcode 7.2.1,启用ARC form
您老是能够本身附加'%',而无需在要附加的字符串中添加任何其余格式说明符,就像这样...
int test = 10; NSString *stringTest = [NSString stringWithFormat:@"%d", test]; stringTest = [stringTest stringByAppendingString:@"%"]; NSLog(@"%@", stringTest);
对于iOS7.0 +
要将答案扩展到可能引发冲突的其余字符,能够选择使用:
- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters
逐步写出来的样子是这样的:
int test = 10; NSString *stringTest = [NSString stringWithFormat:@"%d", test]; stringTest = [[stringTest stringByAppendingString:@"%"] stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet alphanumericCharacterSet]]; stringTest = [stringTest stringByRemovingPercentEncoding]; NSLog(@"percent value of test: %@", stringTest);
或简而言之:
NSLog(@"percent value of test: %@", [[[[NSString stringWithFormat:@"%d", test] stringByAppendingString:@"%"] stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet alphanumericCharacterSet]] stringByRemovingPercentEncoding]);
感谢全部原始贡献者。 但愿这能够帮助。 干杯!