Reputation: 335
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?
Upvotes: 7
Views: 1759
Reputation: 346
Just use
[formatter setMinimumIntegerDigits:1];
That gives at least one digit before decimal point.
Upvotes: 24
Reputation: 364
You need to set up the formatting for significant digits. Check out this thread for some good info: What describes NSNumberFormatter -maximumSignificantDigits?
Upvotes: 1