aprillougheed
asked on
JavaScript Math Rounding to a Decimal
At URL below, I want to round the answer to 2 decimal places.
http://www.netafim-usa.com/calc/shrubclay.htm
Is this possible?
Thanks, April
http://www.netafim-usa.com/calc/shrubclay.htm
Is this possible?
Thanks, April
martinag, why didn't you answer this one? You provided a correct answer!
Of course those three statements can be conbined into one:
i= Math.round(i*100) / 100;
-Josh
Of course those three statements can be conbined into one:
i= Math.round(i*100) / 100;
-Josh
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It's the only way that I have seen!
It's actually pretty simple as it is. They probably figured it wasn't worth implementing a number of decimal places since it's so easy to get around it.
-Josh
It's actually pretty simple as it is. They probably figured it wasn't worth implementing a number of decimal places since it's so easy to get around it.
-Josh
ASKER
I'm sure my question was understood. I have a formula that returns a value like
4.44444444444
and I want it to return
4.44
How does the formula accomplish this?
4.44444444444
and I want it to return
4.44
How does the formula accomplish this?
It translates 4.44444444444 to 444.444444444 by multiplying it by 100. Then it rounds it normally to the nearest integer: 444, and then it divides by 100: 4.44
-josh
-josh
ASKER
Thanks again Josh.
I really appreciate the extra explanation.
I really appreciate the extra explanation.
Note:
55.5555 * 100 = 5555.55
Math.round(5555.55) returns 5556
5556 / 100 = 55.56
Math.round() rounds down if the first decimal is 4 or below, up if it is five or higher.
Martin
55.5555 * 100 = 5555.55
Math.round(5555.55) returns 5556
5556 / 100 = 55.56
Math.round() rounds down if the first decimal is 4 or below, up if it is five or higher.
Martin
i *= 100;
i = Math.round(i);
i /= 100;
Maybe there's some better way? I have no idea.
Martin