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.
Discussions

Rounding to 2 decimal places? - Unity Engine - Unity Discussions
I have a timer and I’ve attached ... but all I’m getting is the whole number instead of decimals. How can i round TimeTaken to 2 decimal places? I tried TimeTaken = Mathf.Round(TimeTaken/100); and the timer completely froze. :neutral: I’d appreciate any help.... More on discussions.unity.com
🌐 discussions.unity.com
0
November 17, 2013
How do you round a number to N decimal places - Support - Kotlin Discussions
So I saw this post about my problem(Print floats with certain amount of decimal numbers) And I was wondering I it possible to use a methood or something else rather “%.2f”.format(value) in order to achive the same thing More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
2
August 2, 2018
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
JavaScript math, round to two decimal places - Stack Overflow
Downvote, because it's very inefficient ... than Math.round) jsperf.com/round-numbers-to-2-digits 2015-08-06T16:29:49.357Z+00:00 ... Seriously enough, the point of this was to answer the initial question, which this answer does. If you have a better answer by all means add it, but nitpicking a 6 year old answer to how to round to 2 decimal places because it ... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

Can you round whole numbers to two decimal places?
Yes, you can express whole numbers to two decimal places by appending zeros. For example, 5 expressed to two decimal places is $5.00$. This is particularly useful in contexts involving money or measurements. Vedantu ensures students understand how and when to apply this format in their answers.
🌐
vedantu.com
vedantu.com › maths › correct to two decimal places: meaning, rules & examples
Correct to Two Decimal Places: Step-by-Step Guide & Examples
What is the significance of rounding numbers to two decimal places in mathematics?
Rounding numbers to two decimal places is important for:Ensuring accuracy in reporting and calculationsSimplifying lengthy numbers to make them more readableStandardizing answers when working with money, measurements, or percentages At Vedantu, students learn the value of precision and the proper application of rounding techniques in real-world mathematics.
🌐
vedantu.com
vedantu.com › maths › correct to two decimal places: meaning, rules & examples
Correct to Two Decimal Places: Step-by-Step Guide & Examples
How do you round negative numbers to two decimal places?
Rounding negative numbers to two decimal places follows the same rules as positive numbers. For example, to round $-4.658$:Check the third decimal digit (8). Since it’s 5 or more, round the second decimal digit (5) up by one to get $-4.66$. Vedantu provides worksheets and teacher support to clarify rounding rules with both positive and negative values.
🌐
vedantu.com
vedantu.com › maths › correct to two decimal places: meaning, rules & examples
Correct to Two Decimal Places: Step-by-Step Guide & Examples
🌐
CodeParrot
codeparrot.ai › blogs › javascript-round-to-2-decimal-places-a-complete-guide
JavaScript Round to 2 Decimal Places: A Complete Guide
JavaScript's floating-point arithmetic can cause unexpected results, especially in JavaScript Round to 2 Decimal Places. Adding Number.EPSILON—the smallest possible value that can be added to 1 to yield a result greater than 1—helps mitigate these errors. const roundWithEpsilon = (num) => Math.round((num + Number.EPSILON) * 100) / 100; console.log(roundWithEpsilon(1.005)); // Output: 1.01 console.log(roundWithEpsilon(1.255)); // Output: 1.26
🌐
VEDANTU
vedantu.com › maths › correct to two decimal places: meaning, rules & examples
Correct to Two Decimal Places: Step-by-Step Guide & Examples
January 13, 2023 - When rounding 6.342 to 2 decimal places, examine the third decimal digit (2). As 2 is less than 5, the second decimal digit (4) remains unchanged. Therefore, 6.342 correct to 2 decimal places is 6.34. Vedantu’s live classes explain these concepts using easy-to-follow examples and practice exercises. 5. What is the significance of rounding numbers to two decimal places in mathematics?
🌐
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.
🌐
CalculatorSoup
calculatorsoup.com › calculators › math › roundingnumbers.php
Rounding Numbers Calculator
The tens digit stays the same at 2. Every digit after becomes a zero. Digits after the decimal point are dropped. ... Is that digit greater than or equal to five? Yes, so round up. The tens digit increases by one. Since 9+1=10, you need to carry the 1 and add it to the digit in the hundreds place.
Find elsewhere
🌐
Unity
discussions.unity.com › unity engine
Rounding to 2 decimal places? - Unity Engine - Unity Discussions
November 17, 2013 - I have a timer and I’ve attached this script to it : var TimeTaken : float; function Update () { TimeTaken = Time.time; TimeTaken = Mathf.Round(TimeTaken); } I want it rounded to 2 decimal places but all I’m getting is the whole number instead of decimals.
🌐
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.
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How do you round a number to N decimal places - Support - Kotlin Discussions
August 2, 2018 - So I saw this post about my problem(Print floats with certain amount of decimal numbers) And I was wondering I it possible to use a methood or something else rather “%.2f”.format(value) in order to achive the same thing …
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › round
Math.round() - JavaScript | MDN
console.log(Math.round(0.9)); // Expected output: 1 console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05)); // Expected output: 6 6 5 console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95)); // Expected output: -5 -5 -6
🌐
Cuemath
cuemath.com › numbers › round-to-two-decimal-places
Round to Two Decimal Places - Meaning | Round to 2 Decimal Places
Decimal numbers are often rounded off to make them easier to understand and use in calculations. Rounding a decimal number to two decimal places is also known as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be ...
🌐
Adobe Support Community
community.adobe.com › home › app communities › acrobat › questions › round to 2 decimal places
Round to 2 decimal places
June 1, 2020 - I did do this in the end, I found it after I posted. But as it just truncates the number after 2 decimal places, I was hoping to see if there was a way to round. ... Both options I posted do round the value. But if you are having issue with it, then Try67 posted a mathematical formula for rounding.
🌐
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 ... console.log(roundToTwoDecimalPlaces(123)); // Output: "123.00" Use toFixed() when you need a string representation of the rounded number....
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)

🌐
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 - Syntax: rounded_number = ... <script type="text/javascript"> function roundOff() { pi = 3.14159265359; twoPlaces = Math.round(pi * 100) / 100; document.querySelector('.output').textContent = twoPlaces; } </script> </body> ...
🌐
UiPath Community
forum.uipath.com › help › studio
How To Round Up Two Decimal Places - Studio - UiPath Community Forum
July 19, 2021 - Hi, I’ve been trying to use Math.Round, but noticed that whenever I do it two two decimal places it always rounds down, and whenever I attempted to use Math.Ceiling, it will always round to the nearest interger, I was w…
🌐
Favtutor
favtutor.com › articles › round-to-two-decimal-places-javascript
Round to 2 Decimal Places in JavaScript (with code)
December 14, 2023 - It rounds the number to the nearest integer and returns the output. ... 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.
🌐
IronPDF
ironpdf.com › ironpdf blog › .net help › csharp-round-to-2-decimal-places
C# Round to 2 Decimal Places (How It Works For Developers)
July 29, 2025 - The second argument for Math.Round specifies that the rounding should be done to two decimal places. One important aspect of rounding is how to handle cases where the number is exactly at the midpoint between two possible rounded values.