Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript toFixed Not Rounding

Tags:

javascript

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)); 
like image 596
gandjyar Avatar asked Apr 04 '12 16:04

gandjyar


People also ask

Does toFixed round JavaScript?

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.

How do I use toFixed without rounding?

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.

Does toFixed round or truncate?

The toFixed() method converts a number to a string. The toFixed() method rounds the string to a specified number of decimals.

Why is toFixed not working?

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.


2 Answers

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); } 
like image 51
user2823670 Avatar answered Sep 18 '22 15:09

user2823670


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;     } 
like image 29
Shura Avatar answered Sep 22 '22 15:09

Shura