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
🌐
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...
🌐
W3Schools
w3schools.com › jsref › jsref_tofixed.asp
W3Schools.com
cssText getPropertyPriority() ... num = 5.56789; let n = num.toFixed(2); Try it Yourself » · More examples below · The toFixed() method converts a number to a string....
Discussions

how to round to 2 decimal places with typescript?
It's not a typescript question, it's a javascript question (maybe even just a math question). But what you are looking for is the toFixed() method. (5).toFixed(2) // returns the string "5.00" https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed btw : your first example "123.45 => should return 123.46" there should be no rounding here it should return "123.45", and toFixed() will take care of rounding for you. More on reddit.com
🌐 r/typescript
9
1
December 25, 2019
rounding to 2 decimal places
🌐 forum.jquery.com
Rounding float numbers to two decimals - CS Principles - Code.org Professional Learning Community
I had students build a coin flipping simulation and at the end I wanted them to give the percentage of each option. However when it displays the number, it creates values like for example 53.25678900004. I would like to reduce that to only display two decimals. More on forum.code.org
🌐 forum.code.org
0
January 10, 2019
How do I round a JavaScript number to 2 decimal places?
Well that probably tells you everything you need to know about my school performance 🤣 Interestingly, even 64bit Excel won't accept 1511000000005.2148, it will only take 2dp no matter what you do. 5.2148 is, of course fine. And yes, it rounds the shorter number to 5.21 as you expect. More on discourse.nodered.org
🌐 discourse.nodered.org
1
0
August 13, 2022
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.

🌐
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.
🌐
Edureka Community
edureka.co › home › community › categories › web development › jquery - round to 2 decimal places and calculate...
jQuery - Round to 2 decimal places and calculate with that number | Edureka Community
August 1, 2022 - How to round the tax and brutto values to 2 decimal places... I have tried to use .toFixed(2) but that ... (); $("#tax").val(brutto - netto); });
Find elsewhere
🌐
Peterlunch
peterlunch.com › snippets › javascript-round
How to round to decimal places in JavaScript?
June 12, 2021 - Unlike many other languages JavaScript does not offer a built-in method to round a number to a specified number of decimal places. The built in Math.round() function only rounds numbers up or down to whole numbers.
🌐
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.
🌐
SitePoint
sitepoint.com › blog › javascript › a guide to rounding numbers in javascript
A Guide to Rounding Numbers in JavaScript — SitePoint
November 13, 2024 - A common use case for rounding to a set number of decimal places is when dealing with currency — for example, if you want to provide the price of something in US dollars to the nearest cent. Let’s say you had an ecommerce site that was running a promotion of 15% off anything in the shopping cart. The discounted price might need rounding before it’s displayed: const item1Price = 2.99 const item2Price = 4.99 const item3Price = 6.20 const totalPrice = item1Price + item2Price + item3Price const discountedPrice = 0.85 * totalPrice << 12.052999999999999
🌐
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.
🌐
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
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....
🌐
Medium
medium.com › @python-javascript-php-html-css › rounding-numbers-in-javascript-to-two-decimal-places-af1350390f26
JavaScript Rounding Numbers to Two Decimal Places
August 24, 2024 - This means Math.round(-1.5) will round to -2, Math.ceil(-1.5) to -1, and Math.floor(-1.5) to -2. ... As we’ve explored, rounding numbers in JavaScript is not just about applying a method; it’s about understanding the context in which these ...
🌐
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.log(roundToTwoDecimalPlaces(123)); // Output: "123.00"
🌐
Code.org
forum.code.org › cs principles
Rounding float numbers to two decimals - CS Principles - Code.org Professional Learning Community
January 10, 2019 - I had students build a coin flipping simulation and at the end I wanted them to give the percentage of each option. However when it displays the number, it creates values like for example 53.25678900004. I would like to reduce that to only display two decimals.
🌐
JavaScriptSource
javascriptsource.com › 3-ways-to-round-a-number-to-2-decimal-places-in-javascript
3 Ways To Round a Number to 2 Decimal Places in JavaScript - JavaScriptSource
March 27, 2023 - Rounding a number to a specific decimal place in JavaScript can be accomplished using the .toFixed() method. This method rounds a number to the number of decimal places specified, and returns the result as a string.
🌐
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.