467

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.

DarenW
  • 16,549
  • 7
  • 63
  • 102
Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168

7 Answers7

954

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

mouviciel
  • 66,855
  • 13
  • 106
  • 140
  • I am trying NSLog([NSString stringWithFormat:@"%@%%", self.Ptextfield.text ] ); and the output is jsut the uitextfield.text – Ali Mar 20 '11 at 09:35
  • 2
    `NSLog` treats its first argument as a format string, but you have already formatted the string using `stringWithFormat:`. Just say `NSLog(@"%@%%", self.Ptextfield.text)`. – rob mayoff Jun 20 '13 at 06:12
  • This doesn't work for UILocalNotification. See http://stackoverflow.com/a/27971848/2446178. – JRam13 Jan 15 '15 at 19:53
  • this works, this is my case powerLbl.text = [NSString stringWithFormat:@"Power: %d%%", power]; – dotrinh PM May 21 '22 at 08:23
140

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

NANNAV
  • 4,875
  • 4
  • 32
  • 50
binarybob
  • 3,529
  • 3
  • 23
  • 21
  • And the reason why \% doesn't work is that backslash is the escape character for string literals, so typing \% in your source code actually creates a string containing a single percent character. – gnasher729 Apr 16 '14 at 17:20
17

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

NSLog(@"Test percentage \uFF05");
Resh32
  • 6,500
  • 3
  • 32
  • 40
  • `\uFF05` works for me when it is part of the `NSString` in `UILocalNotification`, not `%%`. Thanks! – Scott Apr 13 '14 at 13:17
  • 4
    That's a "full width percent sign" for use with chinese or japanese characters, very much different from an ordinary percent character. – gnasher729 Apr 16 '14 at 17:19
10

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.

JRam13
  • 1,132
  • 1
  • 15
  • 25
6

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], @"%%"]; 
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
user1483392
  • 71
  • 2
  • 3
4

uese following code.

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

will output: %Bhupi%

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Bhoopi
  • 6,523
  • 3
  • 22
  • 16
0

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!

serge-k
  • 3,394
  • 2
  • 24
  • 55