I'm using javascript to bind to some checkboxes, and the toFixed(2) is not rounding up. Any ideas why it's not rounding? For instance, if the number is 859.385 it's only displaying 859.38 instead of 859.39.
I've also read that the toFixed can round differently depending on which browser you are using, anyone know of a way around this so that my javascript calculations match my php calculations?
var standardprice = parseFloat($('#hsprice_'+this.id.split('_')[1]).val());
var price = parseFloat($('#hprice_'+this.id.split('_')[1]).val());
var discount = parseFloat($('#hdiscount_'+this.id.split('_')[1]).val());
var deposit = parseFloat($('#hdeposit_'+this.id.split('_')[1]).val());
var currSprice = parseFloat($('#hTotalSprice').val());
var currPrice = parseFloat($('#hTotalPrice').val());
var currDiscount = parseFloat($('#hTotalDiscount').val());
var currDeposit = parseFloat($('#hTotalDeposit').val());
currSprice += standardprice;
currPrice += price;
currDiscount += discount;
currDeposit += deposit;
$('#lblTotalSprice').text('$'+addCommas(currSprice.toFixed(2)));
$('#lblTotalPrice').text('$'+addCommas(currPrice.toFixed(2)));
$('#lblTotalDiscount').text('$'+addCommas(currDiscount.toFixed(2)));
$('#lblTotalDeposit').text('$'+addCommas(currDeposit.toFixed(2)));
$('#hTotalSprice').val(currSprice.toFixed(2));
$('#hTotalPrice').val(currPrice.toFixed(2));
$('#hTotalDiscount').val(currDiscount.toFixed(2));
$('#hTotalDeposit').val(currDeposit.toFixed(2));
I have yet to find a number that toFixed10 does wrong. Can anybody else?
Thanks to blg and his answer which pointed me to Mozilla's toFixed10() method.
Using that I came up with this short one liner, which indeed covers all cases mentioned here...
function toFixed( num, precision ) {
return (+(Math.round(+(num + 'e' + precision)) + 'e' + -precision)).toFixed(precision);
}
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