Formatting NSNumber in percent (%) with one decimal using NSNumberFormatter

I am trying to format an 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 indicate that a number should be formatted as a percentage with one decimal place?

+4
source share
3 answers

Sorry, I am not familiar with NSNumberFormatter, but I would do this:

 NSString *finalNumber = [NSString stringWithFormat:@"%.1f%%", [nsnumber floatValue]*100]; 

EDIT

The correct answer is given below, but since this is already accepted as the correct answer, I again provided the code:

 [numberFormatter setNumberStyle:NSNumberFormatterPercentStyle]; [numberFormatter setMinimumFractionDigits:1]; 
+4
source

This works for me:

 [numberFormatter setNumberStyle:NSNumberFormatterPercentStyle]; [numberFormatter setMinimumFractionDigits:1]; 
+13
source

Try

 float percent = 0.305; NSString *formattedPercentText = [NSString stringWithFormat:@"%.1f%%", percent * 100]; 
+4
source

Source: https://habr.com/ru/post/1398248/


All Articles