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?
Both integers and real numbers may be preceded by a "-" or "+" to indicate the sign. -0 is equivalent to 0 and is not a negative number. So you don't have to have any digits before the decimal point and you must have at least 1 digit after.
2. ALL zeroes between non-zero numbers are ALWAYS significant. 3. ALL zeroes which are SIMULTANEOUSLY to the right of the decimal point AND at the end of the number are ALWAYS significant.
Adding a zero before a number tells javascript to interpret it as an octal (base 8) value. According to the MDN documentation on number literals, you can use the following to represent numbers on different base: decimal: numbers starting with any digit other than zero are decimal. binary: numbers starting with 0b or 0B.
Just use
[formatter setMinimumIntegerDigits:1];
That gives at least one digit before decimal point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With