If you want to round the bottom just add 2 decimal places (* 100
), do Math.floor
and then split by 100 again.
To round up you can use the .toFixed()
, that is to say (49.599).toFixed(2)
gives 49.60
but I don’t think that’s what you want.
Thus, a simpler version of how to shorten the decimal places below:
function arredondar(nr) {
if (nr.indexOf(',') != -1) nr = nr.replace(',', '.');
nr = parseFloat(nr) * 100;
return Math.floor(nr) / 100;
}
jsFiddle: http://jsfiddle.net/voxnmfm2/
If you want a function that accepts as argument the home number you can do so:
function arredondar(str, casas) {
if (str.indexOf(',') != -1) str = str.replace(',', '.');
if (!casas) casas = 0;
casas = Math.pow(10, casas);
str = parseFloat(str) * casas;
return Math.floor(str) / casas;
}
A reference here at Sopt on how to handle money in Javascript: http://answall.com/q/11018/129
– Sergio