I am trying to format a NSNumber containing a value of 0.305 as "30.5%".
However, my following code does not work:
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
How can I specify that the number should be formatted as percentage with one decimal place?
This works for me:
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:1];
Edited for the actual correct answer:
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMinimumFractionDigits:1];
Try
float percent = 0.305;
NSString *formattedPercentText = [NSString stringWithFormat:@"%.1f%%", percent * 100];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With