(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
🌐
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.
🌐
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 ...
🌐
W3Schools
w3schools.com › jsref › jsref_tofixed.asp
JavaScript toFixed() Method
The toFixed() method rounds the string to a specified number of decimals.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-math-exercise-5.php
JavaScript Math: Format a number up to specified decimal places - w3resource
The function then uses the "toFixed()" method to format the number n to have 'd' decimal places. ... See the Pen javascript-math-exercise-5 by w3resource (@w3resource) on CodePen.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › number › number formatting
Formatting numeric values in JavaScript - 30 seconds of code
February 14, 2024 - If we want to format a number to a specific locale, we can pass the locale as the first argument to Number.prototype.toLocaleString(). For example, here's how to format a number to use the decimal mark, using the en-US locale. const toDecimalMark = num => num.toLocaleString('en-US'); toDecimalMark(12_305_030_388.9087); // '12,305,030,388.909' When working with currency, it's important to use the appropriate formatting. Luckily, JavaScript's Intl.NumberFormat makes this easy, allowing us to format numbers as currency strings.
🌐
Mikemcl
mikemcl.github.io › decimal.js
decimal.js API
The return value will have dp decimal places (or less if trailing zeros are produced). If dp is omitted then the number of decimal places will default to the current precision setting.
Find elsewhere
🌐
Attacomsian
attacomsian.com › blog › javascript-format-numbers
Format a number to 2 decimal places in JavaScript
November 27, 2022 - You can use the toFixed() method to format a number to 2 decimal places in JavaScript.
🌐
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.
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Intl › NumberFormat
Intl.NumberFormat - JavaScript | MDN
In order to get the format of the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the locales argument: ... const number = 123456.789; // German uses comma as decimal separator and period for thousands console.log(new Intl.NumberFormat("de-DE").format(number)); // 123.456,789 // Arabic in most Arabic speaking countries uses real Arabic digits console.log(new Intl.NumberFormat("ar-EG").format(number)); // ١٢٣٤٥٦٫٧٨٩ // India uses thousands/lakh/crore separators console.log(new Intl.NumberFormat("en-IN").format(number)); // 1,23,456.789 // the nu extension key requests a numbering system, e.g.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Numbers
According to the documentation Math.round and toFixed both round to the nearest number: 0..4 lead down while 5..9 lead up. ... Internally the decimal fraction 6.35 is an endless binary.
🌐
W3docs
w3docs.com › javascript
How to Format a Number with Two Decimals in JavaScript
const formatter = new Intl.NumberFormat('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2, }); console.log(formatter.format(3.005)); // "3.01" console.log(formatter.format(2.345)); // "2.35" ... The alternative of the above method is the toLocaleString method which internally uses the Intl API: ... const format = (num, decimals) => num.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2, }); console.log(format(3.005)); // "3.01" console.log(format(2.345)); // "2.35"
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-format-a-number-with-two-decimals-in-javascript
How to Format a Number with Two Decimals in JavaScript? | GeeksforGeeks
July 29, 2024 - A number, represented as monetary value, creates an impact and becomes much more readable, and that's the reason behind formatting a number as currency. For example, a number, let's say 100000 when represented as $100,000.00 it becomes pretty much understood that it represents a monetary value, and ... The decimal portion of a number in JavaScript can be removed through various methods, including the Math.
🌐
Medium
medium.com › @stheodorejohn › mastering-number-formatting-in-javascript-d72acc0453df
Mastering Number Formatting in JavaScript | by Theodore John.S | Medium
October 18, 2023 - It rounds or truncates the number to the specified precision and returns it as a string. const price = 49.99; const formattedPrice = price.toFixed(2); // Formats price to two 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 - This guide delves into the nitty-gritty of rounding numbers to two decimal places in JavaScript, aiming to equip developers with practical knowledge to handle numeric data proficiently.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-parse-float-with-two-decimal-places-in-javascript
How to Parse Float with Two Decimal Places in JavaScript? | GeeksforGeeks
In Typescript, parsing float with two decimal places refers to mainly converting the float number with two digits after the decimal points. There are various approaches to parse float with two decimal places in TypeScript which are as follows: Table of Content Using Math.round methodUsing toFixed me ... In javascript, formatting numbers to a fixed number of decimal places is a common requirement, particularly in financial calculations, statistical data presentations, and to show in user interfaces.
Published   January 9, 2025