JavaScript math, round to two decimal places [duplicate]

JavaScript math, round to two decimal places [duplicate]

This question already has an answer here:

Round to at most 2 decimal places (only if necessary)

66 answers

I have the following JavaScript syntax:
var discount = Math.round(100 – (price / listprice) * 100);

This rounds up to the whole number. How can I return the result with two decimal places?


Solution 1:

NOTE – See Edit 4 if 3 digit precision is important

var discount = (price / listprice).toFixed(2);

toFixed will round up or down for you depending on the values beyond 2 decimals.



Edit – As mentioned by others this converts the result to a string. To avoid this:

var discount = +((price / listprice).toFixed(2));

Edit 2– As also mentioned in the comments this function fails in some precision, in the case of 1.005 for example it will return 1.00 instead of 1.01. If accuracy to this degree is important I’ve found this answer: Which seems to work well with all the tests I’ve tried.

There is one minor modification required though, the function in the answer linked above returns whole numbers when it rounds to one, so for example 99.004 will return 99 instead of 99.00 which isn’t ideal for displaying prices.

Edit 3 – Seems having the toFixed on the actual return was STILL screwing up some numbers, this final edit appears to work. Geez so many reworks!

   var discount = roundTo((price / listprice), 2);

   function roundTo(n, digits) {
     if (digits === undefined) {
       digits = 0;

     var multiplicator = Math.pow(10, digits);
     n = parseFloat((n * multiplicator).toFixed(11));
     var test =(Math.round(n) / multiplicator);
     return +(test.toFixed(digits));

See Fiddle example here:

Edit 4 – You guys are killing me. Edit 3 fails on negative numbers, without digging into why it’s just easier to deal with turning a negative number positive before doing the rounding, then turning it back before returning the result.

function roundTo(n, digits) {
    var negative = false;
    if (digits === undefined) {
        digits = 0;
        if( n < 0) {
        negative = true;
      n = n * -1;
    var multiplicator = Math.pow(10, digits);
    n = parseFloat((n * multiplicator).toFixed(11));
    n = (Math.round(n) / multiplicator).toFixed(2);
    if( negative ) {    
        n = (n * -1).toFixed(2);
    return n;


Solution 2:

If you use a unary plus to convert a string to a number as documented on MDN.

For example:+discount.toFixed(2)

Solution 3:

The functions Math.round() and .toFixed() is meant to round to the nearest integer. You’ll get incorrect results when dealing with decimals and using the “multiply and divide” method for Math.round() or parameter for .toFixed(). For example, if you try to round 1.005 using Math.round(1.005 * 100) / 100 then you’ll get the result of 1, and 1.00 using .toFixed(2) instead of getting the correct answer of 1.01.

You can use following to solve this issue:

Number(Math.round(100 - (price / listprice) * 100 + 'e2') + 'e-2');

Add .toFixed(2) to get the two decimal places you wanted.

Number(Math.round(100 - (price / listprice) * 100 + 'e2') + 'e-2').toFixed(2);

You could make a function that will handle the rounding for you:

function round(value, decimals) {
    return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);



You can add a round function to Number using prototype. I would not suggest adding .toFixed() here as it would return a string instead of number.

Number.prototype.round = function(decimals) {
    return Number((Math.round(this + "e" + decimals)  + "e-" + decimals));

and use it like this:

var numberToRound = 100 - (price / listprice) * 100;
numberToRound.round(2).toFixed(2); //Converts it to string with two decimals



Solution 4:

To get the result with two decimals, you can do like this :

var discount = Math.round((100 - (price / listprice) * 100) * 100) / 100;

The value to be rounded is multiplied by 100 to keep the first two digits, then we divide by 100 to get the actual result.

Solution 5:

try using discount.toFixed(2);

Solution 6:

The best and simple solution I found is

function round(value, decimals) {
 return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
round(1.005, 2); // 1.01