ChatGPT解决这个技术问题 Extra ChatGPT

How to add percent sign to NSString

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.


m
mouviciel

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


I am trying NSLog([NSString stringWithFormat:@"%@%%", self.Ptextfield.text ] ); and the output is jsut the uitextfield.text
NSLog treats its first argument as a format string, but you have already formatted the string using stringWithFormat:. Just say NSLog(@"%@%%", self.Ptextfield.text).
This doesn't work for UILocalNotification. See stackoverflow.com/a/27971848/2446178.
this works, this is my case powerLbl.text = [NSString stringWithFormat:@"Power: %d%%", power];
N
NANNAV

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


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.
R
Resh32

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

NSLog(@"Test percentage \uFF05");

\uFF05 works for me when it is part of the NSString in UILocalNotification, not %%. Thanks!
That's a "full width percent sign" for use with chinese or japanese characters, very much different from an ordinary percent character.
J
JRam13

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.


A
Alex Cio

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], @"%%"]; 

A
Alex Cio

uese following code.

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

will output: %Bhupi%


s
serge-k

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!