Javascript toFixed Not Rounding -
i'm using javascript bind checkboxes, , tofixed(2)
not rounding up. ideas why it's not rounding? instance, if number 859.385
it's displaying 859.38
instead of 859.39
.
i've read tofixed
can round differently depending on browser using, know of way around javascript calculations match 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 find number tofixed10 wrong. can else?
thanks blg
, his answer pointed me mozilla's tofixed10() method.
using came short 1 liner, indeed covers cases mentioned here...
function tofixed( num, precision ) { return (+(math.round(+(num + 'e' + precision)) + 'e' + -precision)).tofixed(precision); }
Comments
Post a Comment