Search code examples
cocoacocoa-touchnsnumberformatter

No zero before decimal point


I have a function that converts float numbers to string:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
[formatter setMinimumFractionDigits:0];
NSString *finalNumber = [formatter stringFromNumber:[NSNumber numberWithFloat:sendNumber]];
return finalNumber;

After a simple divide operation 1/2 I get the result as .5 but I was hoping to get 0.5.

How can I change that?


Solution

  • You need to set up the formatting for significant digits. Check out this thread for some good info: What describes NSNumberFormatter -maximumSignificantDigits?