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 - These are functions defined by the users themselves. here I have shown some examples below: Example of a user-defined function by using an exponent. It will round the number up and down. “e+2”, 2 is for 2 decimal places.
🌐
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 - let number = 2.123456; let rounded = Math.round((number + Number.EPSILON) * 100) / 100; console.log(rounded); // Output: 2.12 · Adding Number.EPSILON ensures the rounding operation accounts for the floating-point representation, making the operation more reliable. For applications requiring high precision, external libraries like Decimal.js can be invaluable. These libraries are designed to handle decimal numbers more accurately than JavaScript's native Number type, making them ideal for financial applications, among others.
🌐
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. How do you round numbers in JavaScript without built-in methods? If you prefer not to use toFixed() due to its string output, using the multiplication and division method or custom functions with Math.round() provides a purely numeric solution.
Top answer
1 of 12
1032

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)

🌐
Roblog
robiul.dev › round-to-2-decimal-places-in-javascript
How to Round a Number to 2 Decimal Places in JavaScript
May 28, 2023 - Next, we apply the parseFloat() function to the resulting string. This converts the string representation back into a number, effectively rounding it to 2 decimal places.
🌐
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 - By wrapping it with the Number() function, we convert the string back into a number. The result is 5.68, which is a rounded numeric value. This method is particularly useful when you need the benefits of toFixed() for formatting but also want to ensure that the result can be used in calculations without converting it back to a number later on. It combines the best of both worlds, providing clarity in representation while maintaining numeric integrity. Rounding numbers to two decimal places in JavaScript is a fundamental skill that can enhance the user experience in applications, especially in financial contexts.
🌐
Favtutor
favtutor.com › articles › round-to-two-decimal-places-javascript
Round to 2 Decimal Places in JavaScript (with code)
December 14, 2023 - However, as we want to round to two decimal places, we first convert the number to a string using “e+2” to shift the decimal places two positions to the right. We then add “e-2” to shift the decimal places back to their original position.
Find elsewhere
🌐
CodeParrot
codeparrot.ai › blogs › javascript-round-to-2-decimal-places-a-complete-guide
JavaScript Round to 2 Decimal Places: A Complete Guide
These methods are easy to implement ... to the nearest integer. To round to 2 decimal places, you can scale the number, perform the rounding, and scale it back....
🌐
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
const num = 1.005; console.log(Math.round(num * 10 ** 2) / 10 ** 2); // 1 · However, the answer that you would get is 1. This is because of floating point math and because a computer stores data in binary format.
🌐
W3Schools
w3schools.com › jsref › jsref_tofixed.asp
JavaScript toFixed() Method
❮ Previous JavaScript Number Reference Next ❯ · let num = 5.56789; let n = num.toFixed(); Try it Yourself » · let num = 5.56789; let n = num.toFixed(2); Try it Yourself » · More examples below · The toFixed() method converts a number ...
🌐
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 - The toFixed() method formats a number using fixed-point notation. It returns a string representation of the number rounded to a specified number of decimal places.
🌐
MSR
rajamsr.com › home › javascript round to 2 decimal places made easy
JavaScript Round to 2 Decimal Places Made Easy | MSR - Web Dev Simplified
March 10, 2024 - In JavaScript, how to round percentage to 2 decimal places? Use the toFixed() function, which returns a string representation of a number with a fixed number of decimal places.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Numbers
And if we place one more dot, then JavaScript knows that the decimal part is empty and now uses the method. Also could write (123456).toString(36). One of the most used operations when working with numbers is rounding. There are several built-in functions for rounding: ... Rounds down: 3.1 becomes 3, and -1.1 becomes -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 - You can round a number to 2 decimal places by using the Math.round() function. typescriptconst number = 18.154; // The output of this is: 18.15 console.log(Math.round((number + Number.EPSILON) * 100) / 100); You can also round a number by creating ...
🌐
Stack Abuse
stackabuse.com › bytes › rounding-to-two-decimal-places-in-javascript
Rounding to Two Decimal Places in JavaScript
September 27, 2023 - Another way to round to two decimal places in JavaScript is to use the Number.toFixed() method. This method converts a number into a string, rounding to a specified number of decimal places. let num = 3.14159; let roundedNum = Number(num.toFixed(2)); console.log(roundedNum); // Output: 3.14
🌐
Java2Blog
java2blog.com › home › javascript › round to 2 decimal places in javascript
Round to 2 decimal places in JavaScript - Java2Blog
February 8, 2022 - You can use Math.round() function to round number to 2 decimal places in javascript.
🌐
Peterlunch
peterlunch.com › snippets › javascript-round
How to round to decimal places in JavaScript?
June 12, 2021 - As you can see in the example above, by giving the toFixed() method an argument of 2 it will return a the number as a string rounded to 2 decimal places with the value of '123.46".
🌐
Attacomsian
attacomsian.com › blog › javascript-round-numbers
Round a number to 2 decimal places in JavaScript
November 27, 2022 - Alternatively, you can use the toFixed() method to round a number to 2 decimal places in JavaScript. The toFixed() method rounds and formats the number to 2 decimal places: const num = 5.87456 const str = num.toFixed(2) console.log(str) // 5.87 ...