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.

๐ŸŒ
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....
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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?
๐ŸŒ
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 - JavaScript provides several methods to round numbers, but rounding to a specific number of decimal places requires additional steps. Rounding to 2 decimal places involves converting the number to a string representation, manipulating the string, and converting it back to a number.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Favtutor
favtutor.com โ€บ articles โ€บ round-to-two-decimal-places-javascript
Round to 2 Decimal Places in JavaScript (with code)
December 14, 2023 - In this example, we first multiplied the number by 100 to shift the decimal places two positions to the right. We then use the Math.round() function to round the resulting value to the nearest integer.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ rounding-to-two-decimal-places-in-javascript
Rounding to Two Decimal Places in JavaScript
September 27, 2023 - Then, we round to the nearest whole number, and finally divide by 100 to shift the decimal point back to its original position. This leaves us with a number rounded to two decimal places. Heads up! This method works well for most numbers, but it may give unexpected results for certain numbers due to the way JavaScript handles floating point arithmetic.
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)

๐ŸŒ
Squash
squash.io โ€บ how-to-round-to-2-decimal-places-in-javascript
How To Round To 2 Decimal Places In Javascript
Learn how to round numbers in JavaScript to at most 2 decimal places with simple code examples.
๐ŸŒ
Roblog
robiul.dev โ€บ round-to-2-decimal-places-in-javascript
How to Round a Number to 2 Decimal Places in JavaScript
May 28, 2023 - Finally, we multiply the truncated number by 0.01 This multiplication restores the original decimal places, effectively rounding the number to 2 decimal places. Note: it's important to note that this method relies on integer manipulation and may not be suitable for all rounding scenarios. Consider the specific requirements of your use case, such as handling negative numbers or ensuring consistent behavior with rounding rules, when selecting this approach. So far, we have discussed 5 ways of rounding but all of these were using javascript built in methods.
๐ŸŒ
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...
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ javascript-round-numbers
Round a number to 2 decimal places in JavaScript
November 27, 2022 - 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
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_tofixed.asp
JavaScript toFixed() Method
โฎ Previous JavaScript Number ... ยท The toFixed() method converts a number to a string. The toFixed() method rounds the string to a specified number of decimals....
๐ŸŒ
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 - rounded_number = number.toFixed(2) Example: [sourcecode language="html"] <!DOCTYPE html> <html> <head> <title>Round off a number upto 2 decimal place using JavaScript</title> </head> <body> <h1 style="color: green">GeeksforGeeks</h1> <b>Round off a number upto 2 decimal place using JavaScript</b> <p>Original floating point: 3.14159265359</p> <p>Rounding off to 2 decimal places: <span class="output"></span></p> <button onclick="roundOff()">Round off to 2 places</button> <script type="text/javascript"> function roundOff() { pi = 3.14159265359; twoPlaces = pi.toFixed(2); document.querySelector('.output').textContent = twoPlaces; } </script> </body> </html> [/sourcecode] Output:
๐ŸŒ
Medium
medium.com โ€บ @python-javascript-php-html-css โ€บ javascript-rounding-numbers-to-a-maximum-of-two-decimal-places-775b9650d6fb
JavaScript Rounding Numbers to a Maximum of Two Decimal Places
September 29, 2024 - In this guide, weโ€™ll explore a simple and effective method to achieve this rounding requirement in JavaScript. Youโ€™ll learn how to handle various inputs and ensure your numbers are displayed correctly, making your data presentation both precise and professional. ... The scripts provided are designed to round a number to at most two decimal places, ensuring precision and readability in your JavaScript applications.