Use Math.round() :

Math.round(num * 100) / 100

Or to be more specific and to ensure things like 1.005 round correctly, use Number.EPSILON :

Math.round((num + Number.EPSILON) * 100) / 100
Answer from Brian Ustas on Stack Overflow
Top answer
1 of 16
5568

Use Math.round() :

Math.round(num * 100) / 100

Or to be more specific and to ensure things like 1.005 round correctly, use Number.EPSILON :

Math.round((num + Number.EPSILON) * 100) / 100
2 of 16
4380

If the value is a text type:

parseFloat("123.456").toFixed(2);

If the value is a number:

var numb = 123.23454;
numb = numb.toFixed(2);

There is a downside that values like 1.5 will give "1.50" as the output. A fix suggested by @minitech:

var numb = 1.5;
numb = +numb.toFixed(2);
// Note the plus sign that drops any "extra" zeroes at the end.
// It changes the result (which is a string) into a number again (think "0 + foo"),
// which means that it uses only as many digits as necessary.

It seems like Math.round is a better solution. But it is not! In some cases it will not round correctly:

Math.round(1.005 * 100)/100 // Returns 1 instead of expected 1.01!

toFixed() will also not round correctly in some cases (tested in Chrome v.55.0.2883.87)!

Examples:

parseFloat("1.555").toFixed(2); // Returns 1.55 instead of 1.56.
parseFloat("1.5550").toFixed(2); // Returns 1.55 instead of 1.56.
// However, it will return correct result if you round 1.5551.
parseFloat("1.5551").toFixed(2); // Returns 1.56 as expected.

1.3555.toFixed(3) // Returns 1.355 instead of expected 1.356.
// However, it will return correct result if you round 1.35551.
1.35551.toFixed(2); // Returns 1.36 as expected.

I guess, this is because 1.555 is actually something like float 1.55499994 behind the scenes.

Solution 1 is to use a script with required rounding algorithm, for example:

function roundNumber(num, scale) {
  if(!("" + num).includes("e")) {
    return +(Math.round(num + "e+" + scale)  + "e-" + scale);
  } else {
    var arr = ("" + num).split("e");
    var sig = ""
    if(+arr[1] + scale > 0) {
      sig = "+";
    }
    return +(Math.round(+arr[0] + "e" + sig + (+arr[1] + scale)) + "e-" + scale);
  }
}

It is also at Plunker.

Note: This is not a universal solution for everyone. There are several different rounding algorithms. Your implementation can be different, and it depends on your requirements. See also Rounding.

Solution 2 is to avoid front end calculations and pull rounded values from the backend server.

Another possible solution, which is not a bulletproof either.

Math.round((num + Number.EPSILON) * 100) / 100

In some cases, when you round a number like 1.3549999999999998, it will return an incorrect result. It should be 1.35, but the result is 1.36.

🌐
Codedamn
codedamn.com › news › javascript
JavaScript round a number to 2 decimal places (with examples)
December 11, 2022 - What do you understand with the question that “Round a number to x decimal places?” The answer is just round off the given decimal number to x decimal places. for example, round the 5.678 to 2 decimal places. the result will be 5.68.
🌐
CodeParrot
codeparrot.ai › blogs › javascript-round-to-2-decimal-places-a-complete-guide
JavaScript Round to 2 Decimal Places: A Complete Guide
Precision is crucial in JavaScript, and mastering JavaScript Round to 2 Decimal Places is essential for accurate calculations and clean formatting. In this blog, we’ll explore methods like Math.round and Math.floor in JavaScript.
🌐
Medium
medium.com › @ryan_forrester_ › how-to-round-to-2-decimal-places-in-javascript-053a869b2ce8
How to Round to 2 Decimal Places in JavaScript | by ryan | Medium
September 17, 2024 - It returns a string representation of the number rounded to a specified number of decimal places. function roundToTwoDecimalPlaces(num) { return num.toFixed(2); } console.log(roundToTwoDecimalPlaces(123.456)); // Output: "123.46" console.lo...
🌐
W3Schools
w3schools.com › jsref › jsref_tofixed.asp
W3Schools.com
cssText getPropertyPriority() getPropertyValue() item() length parentRule removeProperty() setProperty() JS Conversion · ❮ Previous JavaScript Number Reference Next ❯ · let num = 5.56789; let n = num.toFixed(); Try it Yourself » · let ...
🌐
CoreUI
coreui.io › blog › how-to-round-a-number-to-two-decimal-places-in-javascript
How to round a number to two decimal places in JavaScript · CoreUI
February 21, 2024 - The key to rounding to 2 decimal places is to manipulate the number such that the function applies rounding at the correct decimal position, as illustrated through the methods above.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › round
Math.round() - JavaScript | MDN
If the fractional portion of the argument is greater than 0.5, the argument is rounded to the integer with the next higher absolute value. If it is less than 0.5, the argument is rounded to the integer with the lower absolute value.
🌐
Favtutor
favtutor.com › articles › round-to-two-decimal-places-javascript
Round to 2 Decimal Places in JavaScript (with code)
December 14, 2023 - If we want to round the number to 2 decimal places, we need to multiply the number by 100 and then pass it to the function, and finally divide the rounded value by 100. ... const number = 5.6789; const rounded = Math.round(number * 100) / 100; ...
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toFixed
Number.prototype.toFixed() - JavaScript | MDN
The toFixed() method of Number values returns a string representing this number using fixed-point notation with the specified number of decimal places. function financial(x) { return Number.parseFloat(x).toFixed(2); } console.log(financial(123.456)); // Expected output: "123.46" console.lo...
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript round to 2 decimal places
How to Round a Number to 2 Decimal Places in JavaScript | Delft Stack
March 11, 2025 - This method converts a number into a string representation, keeping a specified number of decimals. ... In this example, the toFixed(2) method is called on the variable number, which holds the value 5.6789.
🌐
Zipy
zipy.ai › blog › how-to-round-to-at-most-two-decimal-places-in-javascript
how to round to at most two decimal places in javascript
April 12, 2024 - This method converts a number into a string, rounding it to a specified number of decimal places. let number = 2.123456; let rounded = number.toFixed(2); console.log(rounded); // Output: "2.12"
🌐
LearnersBucket
learnersbucket.com › home › examples › javascript › learn how to round to 2 decimal places in javascript
Learn how to round to 2 decimal places in javascript - LearnersBucket
September 19, 2019 - To round off any number to any decimal place we can use this method by first multiplying the input number with 10 ^ decimal place, so in our case it is 2 that is Math.round(3.14159265359 * (10 ^ 2)) and then divide it by 10 ^ decimal place like Math.round(3.14159265359 * (10 ^ 2)) / (10 ^ 2) so it ...
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Numbers
For instance, we have 1.2345 and want to round it to 2 digits, getting only 1.23. ... For example, to round the number to the 2nd digit after the decimal, we can multiply the number by 100, call the rounding function ...
Top answer
1 of 12
1031

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.

Example: http://jsfiddle.net/calder12/tv9HY/

Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

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: https://stackoverflow.com/a/32605063/1726511 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: https://jsfiddle.net/calder12/3Lbhfy5s/

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(digits);
    if (negative) {
        n = (n * -1).toFixed(digits);
    }
    return n;
}

Fiddle: https://jsfiddle.net/3Lbhfy5s/79/

2 of 12
172

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

For example:+discount.toFixed(2)

🌐
Tim Mousk
timmousk.com › blog › javascript-round-to-2-decimal-places
How to round to 2 decimal places in JavaScript? – Tim Mouskhelichvili
March 11, 2023 - typescriptconst number = 18.154; function roundTo(n, place) { return +(Math.round(n + "e+" + place) + "e-" + place); } // The output of this is: 18.15 console.log(roundTo(number, 2));
🌐
Sentry
sentry.io › sentry answers › javascript › how to round to at most two decimal places in javascript
How to round to at most two decimal places in JavaScript | Sentry
To round a number to two decimal places at most, multiply the number by 10 to the power of 2. This moves the decimal place to the position where you want rounding to occur. Then round the number to the nearest integer using Math.round() and ...
🌐
Squash
squash.io › how-to-round-to-2-decimal-places-in-javascript
How To Round To 2 Decimal Places In Javascript
In the above code, we first declare a variable number and assign it a value of 5.6789. We then use the toFixed() method with the argument 2 to round the number to two decimal places.
🌐
Roblog
robiul.dev › round-to-2-decimal-places-in-javascript
How to Round a Number to 2 Decimal Places in JavaScript
May 28, 2023 - By rounding the average temperature to 2 decimal places, the displayed value becomes 25.94°C. In this scenario rounding the number helps to simplify the temperature representation, making it easier to read and understand without sacrificing ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › round-off-a-number-upto-2-decimal-place-using-javascript
Round off a number upto 2 decimal place using JavaScript - GeeksforGeeks
January 20, 2020 - The toFixed() method is used with a value of 2 to round off a number upto 2 decimal places. Syntax: rounded_number = number.toFixed(2) Example: [sourcecode language="html"] <!DOCTYPE html> <html> <head> <title>Round off a number upto 2 decimal ...
🌐
Peterlunch
peterlunch.com › snippets › javascript-round
How to round to decimal places in JavaScript?
June 12, 2021 - In the example above, we take the number 123.4567 and multiply it by 100 inside of the brackets. Then we divide that by 100 to give you a lovely number rounded to 2 decimal places all thanks to JavaScript and some basic math.