Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 973
Adami Avatar asked Feb 06 '13 00:02

Adami


People also ask

Do you need a 0 before the decimal point?

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.

Does the zero at the end of a decimal matter?

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.

Why do we put zero before number?

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.


1 Answers

Just use

[formatter setMinimumIntegerDigits:1];

That gives at least one digit before decimal point.

like image 200
mefik Avatar answered Sep 28 '22 20:09

mefik