How to add percent sign to NSString

Objective CNsstringString Literals

Objective C Problem Overview


I want to have a percentage sign in my string after a digit. Something like this: 75%.

How can I have this done? I tried:

[NSString stringWithFormat:@"%d\%", someDigit];

But it didn't work for me.

Objective C Solutions


Solution 1 - Objective C

The code for percent sign in NSString format is %%. This is also true for NSLog() and printf() formats.

Solution 2 - Objective C

The escape code for a percent sign is "%%", so your code would look like this

[NSString stringWithFormat:@"%d%%", someDigit];

Also, all the other format specifiers can be found at Conceptual Strings Articles

Solution 3 - Objective C

If that helps in some cases, it is possible to use the unicode character:

NSLog(@"Test percentage \uFF05");

Solution 4 - Objective C

The accepted answer doesn't work for UILocalNotification. For some reason, %%%% (4 percent signs) or the unicode character '\uFF05' only work for this.

So to recap, when formatting your string you may use %%. However, if your string is part of a UILocalNotification, use %%%% or \uFF05.

Solution 5 - Objective C

seems if %% followed with a %@, the NSString will go to some strange codes try this and this worked for me

NSString *str = [NSString stringWithFormat:@"%@%@%@", @"%%", 
                 [textfield text], @"%%"]; 

Solution 6 - Objective C

uese following code.

 NSString *searchText = @"Bhupi"
 NSString *formatedSearchText = [NSString stringWithFormat:@"%%%@%%",searchText];

will output: %Bhupi%

Solution 7 - Objective C

iOS 9.2.1, Xcode 7.2.1, ARC enabled

You can always append the '%' by itself without any other format specifiers in the string you are appending, like so...

int test = 10;

NSString *stringTest = [NSString stringWithFormat:@"%d", test];
stringTest = [stringTest stringByAppendingString:@"%"];
NSLog(@"%@", stringTest);

For iOS7.0+

To expand the answer to other characters that might cause you conflict you may choose to use:

- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters

Written out step by step it looks like this:

int test = 10;
            
NSString *stringTest = [NSString stringWithFormat:@"%d", test];
stringTest = [[stringTest stringByAppendingString:@"%"] 
             stringByAddingPercentEncodingWithAllowedCharacters:
             [NSCharacterSet alphanumericCharacterSet]];
stringTest = [stringTest stringByRemovingPercentEncoding];

NSLog(@"percent value of test: %@", stringTest);

Or short hand:

NSLog(@"percent value of test: %@", [[[[NSString stringWithFormat:@"%d", test] 
stringByAppendingString:@"%"] stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]] stringByRemovingPercentEncoding]);

Thanks to all the original contributors. Hope this helps. Cheers!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIlya SuzdalnitskiView Question on Stackoverflow
Solution 1 - Objective CmouvicielView Answer on Stackoverflow
Solution 2 - Objective CbinarybobView Answer on Stackoverflow
Solution 3 - Objective CResh32View Answer on Stackoverflow
Solution 4 - Objective CJRam13View Answer on Stackoverflow
Solution 5 - Objective Cuser1483392View Answer on Stackoverflow
Solution 6 - Objective CBhoopiView Answer on Stackoverflow
Solution 7 - Objective Cserge-kView Answer on Stackoverflow