Search code examples
iosobjective-cnsnumberformatterfractions

Issues with setMaximumFractionDigits


setMaximumFractionDigits is not working with the following code.

NSString *input=@"80.90";
NSNumberFormatter *numberFormatter=[[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2];
NSNumber *number=[numberFormatter numberFromString:input];

number gets converted to 80.90000000000001. but I have put a limit to 2 decimal places. So, how do I get number as 80.90, instead of 80.90000000000001


Solution

  • setMaximumFractionDigits and setMinimumFractionDigits used to formatting a number. or in other words it get applied while converting a number into string i.e. stringFromNumber. So it will not get applied when you are using numberFromString