im getting a little issue here. Im trying to do a simple math, but im forgetting something.
Here's what:
var valorOriginal = 9.90;
var discount = Math.round(valorOriginal*12*0.25);
var anual = Math.round(valorOriginal*12)-discount;
alert(anual);
If you do this math on calc, it will give you 89.1
, but im just getting 89 rounded. Whats wrong?
Thanks!
im getting a little issue here. Im trying to do a simple math, but im forgetting something.
Here's what:
var valorOriginal = 9.90;
var discount = Math.round(valorOriginal*12*0.25);
var anual = Math.round(valorOriginal*12)-discount;
alert(anual);
If you do this math on calc, it will give you 89.1
, but im just getting 89 rounded. Whats wrong?
Thanks!
Share Improve this question edited Jan 8, 2012 at 13:16 T.J. Crowder 1.1m200 gold badges2k silver badges1.9k bronze badges asked Jan 8, 2012 at 13:11 Lucas VeigaLucas Veiga 1,7937 gold badges27 silver badges48 bronze badges 4- 2 Do you still need to finish translating some of that code? – Gareth Commented Jan 8, 2012 at 13:13
- is desconto a misspelling for discount? or is it another variable defined elsewhere? – Fabrizio Calderan Commented Jan 8, 2012 at 13:14
- Why do you round() if you don't want to round? – Adam Zalcman Commented Jan 8, 2012 at 13:14
-
Why does it surprise you that you're getting 89 instead of 89.1? As @Jani points out in his answer, that's exactly what
Math.round
is for. – T.J. Crowder Commented Jan 8, 2012 at 13:15
6 Answers
Reset to default 6That's exactly what Round method will do for you.
Rounding the number to closest integer.
Math.round rounds up to the nearest integer value. So you will loose the decimal points. If you want the decimal, you are probably after toFixed().
var valorOriginal = 9.90;
var discount = (valorOriginal*12*0.25);
var anual = (valorOriginal*12)-discount;
alert( anual.toFixed(2) );
DEMO
You need to add this method:
Math.roundMoney = function(number) {
return Math.round(number * 100) / 100;
};
And use it like this:
var valorOriginal = 9.90;
var discount = Math.roundMoney(valorOriginal*12*0.25);
var anual = Math.roundMoney(valorOriginal*12)-discount;
alert(anual);
anual is a variable obtained by a difference between two rounded values, so the result should be rounded too
The JavaScript Math.round function will round on 0 decimals and your calculator will round on 2 decimals.
So you need to make a little trick:
Math.nativeRound = Math.round;
Math.round = function(i, iDecimals) {
if (!iDecimals)
return Math.nativeRound(i);
else
return Math.nativeRound(i * Math.pow(10, Math.abs(iDecimals))) / Math.pow(10, Math.abs(iDecimals));
};
var valorOriginal = 9.90;
var discount = Math.round(valorOriginal*12*0.25, 2);
var anual = Math.round(valorOriginal*12, 2)-discount;
alert(anual);
Example: http://jsfiddle/p34EC/
Maybe you should use toFixed
method which preserving ( rounding to ) a count of decimal numbers passed in arguments and you can also shorten this operation to:
var valorOriginal = 9.90;
( ( valorOriginal - valorOriginal / 10 ) * 10 ).toFixed(2) //"89.10"