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));
Note. The toFixed() method will round the resulting value if necessary. The toFixed() method will pad the resulting value with 0's if there are not enough decimal places in the original number. The toFixed() method does not change the value of the original number.
The idea is to convert the number into string (via toString()) and use the RegExp to truncate the extra zeros (without rounding). Next, we find out the position of decimal dot (if any), and pad up the zeros for exactly n decimal places.
The toFixed() method converts a number to a string. The toFixed() method rounds the string to a specified number of decimals.
The "toFixed is not a function" error occurs when the toFixed() method is called on a value that is not a number . To solve the error, either convert the value to a number before calling the toFixed method or only call the method on numbers.
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); }
I made this to use in all financial data as a best rounding function. You can test it on all problematic numbers. Javascript allows some sort of precision, so I used it to make almost every number be rounded as expected.
function roundTo(n, digits) { if (digits === undefined) { digits = 0; } var multiplicator = Math.pow(10, digits); n = parseFloat((n * multiplicator).toFixed(11)); return Math.round(n) / multiplicator; }
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