(Math.round(num * 100) / 100).toFixed(2);

Live Demo

var num1 = "1";
document.getElementById('num1').innerHTML = (Math.round(num1 * 100) / 100).toFixed(2);

var num2 = "1.341";
document.getElementById('num2').innerHTML = (Math.round(num2 * 100) / 100).toFixed(2);

var num3 = "1.345";
document.getElementById('num3').innerHTML = (Math.round(num3 * 100) / 100).toFixed(2);
span {
    border: 1px solid #000;
    margin: 5px;
    padding: 5px;
}
<span id="num1"></span>
<span id="num2"></span>
<span id="num3"></span>

Note that it will round to 2 decimal places, so the input 1.346 will return 1.35.

Answer from jrn.ak on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ howto โ€บ howto_js_format_number_dec.asp
How To Format a Number with Two Decimals
You can use the toFixed() method to format a number to only show two decimals. Note that the result is rounded (shows 5.57 instead of 5.56): let num = 5.56789; let n = num.toFixed(2); // 5.57 Try it Yourself ยป ยท If you want to display three ...
๐ŸŒ
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-format-numbers
Format a number to 2 decimal places in JavaScript
November 27, 2022 - The toFixed() method takes a number as input, representing the number of digits to appear after the decimal point, and returns a formatted string representing the given number. const num1 = 12.865 const res1 = num1.toFixed(2) console.log(res1) ...
๐ŸŒ
W3docs
w3docs.com โ€บ javascript
How to Format a Number with Two Decimals in JavaScript
There are several methods used to format a number with two decimals in JavaScript. Letโ€™s see the difference between the methods. You can use the Intl.NumberFormat constructor. It is supported in major browsers and in Node.js: ... const formatter ...
๐ŸŒ
CodeParrot
codeparrot.ai โ€บ blogs โ€บ javascript-round-to-2-decimal-places-a-complete-guide
JavaScript Round to 2 Decimal Places: A Complete Guide
Number.EPSILON accounts for minute inaccuracies in floating-point calculations, improving precision in JavaScript Round to 2 Decimal Places for tricky numbers like 1.005 or 1.255. The Intl.NumberFormat API is a versatile constructor for formatting numbers as per locale-specific conventions.
Find elsewhere
๐ŸŒ
TestMu AI Community
community.testmu.ai โ€บ ask a question
JavaScript: Format Number to 2 Decimal Places - TestMu AI Community
September 6, 2024 - How can I format a number in JavaScript to always show 2 decimal places, rounding where applicable? For example, I want to achieve the following: number display 1 1.00 1.341 1.34 1.345 1.35 Iโ€™ve been using parseFloat(num).toFixed(2);, but itโ€™s displaying 1 as 1 instead of 1.00.
๐ŸŒ
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 - This method rounds the number and formats it according to the specified locale, making it invaluable for international applications. ... 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 ...
๐ŸŒ
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.
๐ŸŒ
W3docs
w3docs.com โ€บ javascript
How to Format Number with Two Decimals
var num1 = "1"; console.log((Math.round(num1 * 100) / 100).toFixed(2)); var num2 = "1.586"; console.log((Math.round(num2 * 100) / 100).toFixed(2)); var num3 = "1.756324"; console.log((Math.round(num3 * 100) / 100).toFixed(2)); ... However, this ...
Top answer
1 of 16
1156

To format a number using fixed-point notation, you can simply use the toFixed method:

Copy(10.8).toFixed(2); // "10.80"

var num = 2.4;
alert(num.toFixed(2)); // "2.40"

Note that toFixed() returns a string.

IMPORTANT: Note that toFixed does not round 90% of the time, it will return the rounded value, but for many cases, it doesn't work.

For instance:

2.005.toFixed(2) === "2.00"

UPDATE:

Nowadays, you can use the Intl.NumberFormat constructor. It's part of the ECMAScript Internationalization API Specification (ECMA402). It has pretty good browser support, including even IE11, and it is fully supported in Node.js.

Copyconst formatter = new Intl.NumberFormat('en-US', {
   minimumFractionDigits: 2,      
   maximumFractionDigits: 2,
});

console.log(formatter.format(2.005)); // "2.01"
console.log(formatter.format(1.345)); // "1.35"
Run code snippetEdit code snippet Hide Results Copy to answer Expand

You can alternatively use the toLocaleString method, which internally will use the Intl API:

Copyconst format = (num, decimals) => num.toLocaleString('en-US', {
   minimumFractionDigits: 2,      
   maximumFractionDigits: 2,
});


console.log(format(2.005)); // "2.01"
console.log(format(1.345)); // "1.35"
Run code snippetEdit code snippet Hide Results Copy to answer Expand

This API also provides you a wide variety of options to format, like thousand separators, currency symbols, etc.

2 of 16
112

This is an old topic but still top-ranked Google results and the solutions offered share the same floating point decimals issue. Here is the (very generic) function I use, thanks to MDN:

Copyfunction round(value, exp) {
  if (typeof exp === 'undefined' || +exp === 0)
    return Math.round(value);

  value = +value;
  exp = +exp;

  if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0))
    return NaN;

  // Shift
  value = value.toString().split('e');
  value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)));

  // Shift back
  value = value.toString().split('e');
  return +(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp));
}

As we can see, we don't get these issues:

Copyround(1.275, 2);   // Returns 1.28
round(1.27499, 2); // Returns 1.27

This genericity also provides some cool stuff:

Copyround(1234.5678, -2);   // Returns 1200
round(1.2345678e+2, 2); // Returns 123.46
round("123.45");        // Returns 123

Now, to answer the OP's question, one has to type:

Copyround(10.8034, 2).toFixed(2); // Returns "10.80"
round(10.8, 2).toFixed(2);    // Returns "10.80"

Or, for a more concise, less generic function:

Copyfunction round2Fixed(value) {
  value = +value;

  if (isNaN(value))
    return NaN;

  // Shift
  value = value.toString().split('e');
  value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + 2) : 2)));

  // Shift back
  value = value.toString().split('e');
  return (+(value[0] + 'e' + (value[1] ? (+value[1] - 2) : -2))).toFixed(2);
}

You can call it with:

Copyround2Fixed(10.8034); // Returns "10.80"
round2Fixed(10.8);    // Returns "10.80"

Various examples and tests (thanks to @t-j-crowder!):

Show code snippet

Copyfunction round(value, exp) {
  if (typeof exp === 'undefined' || +exp === 0)
    return Math.round(value);

  value = +value;
  exp = +exp;

  if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0))
    return NaN;

  // Shift
  value = value.toString().split('e');
  value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)));

  // Shift back
  value = value.toString().split('e');
  return +(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp));
}
function naive(value, exp) {
  if (!exp) {
    return Math.round(value);
  }
  var pow = Math.pow(10, exp);
  return Math.round(value * pow) / pow;
}
function test(val, places) {
  subtest(val, places);
  val = typeof val === "string" ? "-" + val : -val;
  subtest(val, places);
}
function subtest(val, places) {
  var placesOrZero = places || 0;
  var naiveResult = naive(val, places);
  var roundResult = round(val, places);
  if (placesOrZero >= 0) {
    naiveResult = naiveResult.toFixed(placesOrZero);
    roundResult = roundResult.toFixed(placesOrZero);
  } else {
    naiveResult = naiveResult.toString();
    roundResult = roundResult.toString();
  }
  $("<tr>")
    .append($("<td>").text(JSON.stringify(val)))
    .append($("<td>").text(placesOrZero))
    .append($("<td>").text(naiveResult))
    .append($("<td>").text(roundResult))
    .appendTo("#results");
}
test(0.565, 2);
test(0.575, 2);
test(0.585, 2);
test(1.275, 2);
test(1.27499, 2);
test(1234.5678, -2);
test(1.2345678e+2, 2);
test("123.45");
test(10.8034, 2);
test(10.8, 2);
test(1.005, 2);
test(1.0005, 2);
Copytable {
  border-collapse: collapse;
}
table, td, th {
  border: 1px solid #ddd;
}
td, th {
  padding: 4px;
}
th {
  font-weight: normal;
  font-family: sans-serif;
}
td {
  font-family: monospace;
}
Copy<table>
  <thead>
    <tr>
      <th>Input</th>
      <th>Places</th>
      <th>Naive</th>
      <th>Thorough</th>
    </tr>
  </thead>
  <tbody id="results">
  </tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run code snippetEdit code snippet Hide Results Copy to answer Expand

๐ŸŒ
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.
๐ŸŒ
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 - Rounding numbers to a specific number of decimal places is a common requirement in JavaScript, especially for financial calculations, user input validation, and data formatting. This article explores various techniques to round numbers to 2 decimal places in JavaScript and provide practical ...
๐ŸŒ
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.
๐ŸŒ
Coding Beauty
codingbeautydev.com โ€บ home โ€บ posts โ€บ how to round a number to 2 decimal places in javascript
How to Round a Number to 2 Decimal Places in JavaScript
December 11, 2022 - To round a number to 2 decimal places in JavaScript, call the toFixed() method on the number, i.e., num.toFixed(2). toFixed() will round and format the number to 2 decimal places.
๐ŸŒ
Medium
medium.com โ€บ @noffybarudwale โ€บ javascript-format-numbers-with-commas-and-decimals-86b68ec5b180
JavaScript : Format numbers with commas and decimals. | by Nofij Barudwale | Medium
October 13, 2021 - There are many different ways of printing an integer with a comma as a thousands separators in JavaScript. I found a good function that details how to and thought I would reproduce it here. It basically takes any number and turns it into formatted string with the thousands separated by commas and decimals.
๐ŸŒ
Favtutor
favtutor.com โ€บ articles โ€บ round-to-two-decimal-places-javascript
Round to 2 Decimal Places in JavaScript (with code)
December 14, 2023 - We then use the Math.round() function to round the resulting value to the nearest integer. Finally, we divide the rounded value by 100 to shift the decimal places back to their original position.