Formatting number as percentage

Mustafa picture Mustafa · Aug 11, 2010 · Viewed 7.9k times · Source

I don't understand how NSNumberFormatterPercentStyle works!

Example:

NSNumber *number = [NSNumber numberWithFloat:90.5];
NSNumberFormatter *percentageFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[percentageFormatter setNumberStyle:NSNumberFormatterPercentStyle];

NSString *strNumber = [percentageFormatter stringFromNumber:numbericString];
NSLog (strNumber);  // Output: 9,050%

I simply want to show "90.5%" (respecting NSLocal). What is NSNumberFormatterPercentStyle doing with 90.5, and why? And, how can i get my desired result!?

Answer

Pulkit Goyal picture Pulkit Goyal · Sep 4, 2013

Just set the multiplier to 1 if your numbers are already in percentage.

numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMaximumFractionDigits:0];
[numberFormatter setMultiplier:@1];