No zero to decimal point

I have a function that converts floating point 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 1/2 division operation, I get the result as .5, but I was hoping to get 0.5.

How to change this?

+5
source share
2 answers

You need to configure formatting for significant digits. Check out this topic for some good info: What does NSNumberFormatter -maximumSignificantDigits describe?

0
source

Just use

[formatter setMinimumIntegerDigits:1];

This gives at least one digit to the decimal point.

+13
source

All Articles