How can I round a number in JavaScript? .toFixed() returns a string?
Number.prototype.toFixed
is a function designed to format a number before printing it out. It's from the family of toString
, toExponential
and toPrecision
.
To round a number, you would do this:
someNumber = 42.008;someNumber = Math.round( someNumber * 1e2 ) / 1e2;someNumber === 42.01;// if you need 3 digits, replace 1e2 with 1e3 etc.// or just copypaste this function to your code:function toFixedNumber(num, digits, base){ var pow = Math.pow(base||10, digits); return Math.round(num*pow) / pow;}
.
Or if you want a “native-like” function, you can extend the prototype:
Number.prototype.toFixedNumber = function(digits, base){ var pow = Math.pow(base||10, digits); return Math.round(this*pow) / pow;}
someNumber = 42.008;someNumber = someNumber.toFixedNumber(2);someNumber === 42.01;//or even hexadecimalsomeNumber = 0xAF309/256 //which is af3.09someNumber = someNumber.toFixedNumber(1, 16);someNumber.toString(16) === "af3.1";
However, bear in mind that polluting the prototype is considered bad when you're writing a module, as modules shouldn't have any side effects. So, for a module, use the first function.
It returns a string because 0.1, and powers thereof (which are used to display decimal fractions), are not representable (at least not with full accuracy) in binary floating-point systems.
For example, 0.1 is really 0.1000000000000000055511151231257827021181583404541015625, and 0.01 is really 0.01000000000000000020816681711721685132943093776702880859375. (Thanks to BigDecimal
for proving my point. :-P)
Therefore (absent a decimal floating point or rational number type), outputting it as a string is the only way to get it trimmed to exactly the precision required for display.
I've solved this problem by changing this:
someNumber = someNumber.toFixed(2)
...to this:
someNumber = +someNumber.toFixed(2);
However this will convert the number to a string and parse it again, which will have a significant impact on performance. If you care about performance or type safety, check the the other answers as well.