How to round to at most 2 decimal places, if necessary
Categories:
How to Round Numbers to At Most Two Decimal Places in JavaScript

Learn various JavaScript techniques to round numbers to a maximum of two decimal places, ensuring precision while avoiding unnecessary trailing zeros.
Rounding numbers is a common task in programming, especially when dealing with financial calculations, measurements, or displaying user-friendly output. In JavaScript, you often need to round a number to a specific number of decimal places, but sometimes you also want to avoid showing trailing zeros if the number is already exact or has fewer decimal places than the maximum specified. This article explores several robust methods to achieve this, focusing on rounding to at most two decimal places.
Understanding the Challenge
The core challenge lies in differentiating between rounding to exactly two decimal places and rounding to at most two decimal places. For instance, 10.00
is exactly two decimal places, but 10.5
is one, and 10
is zero. If we simply use toFixed(2)
, 10
becomes "10.00"
, which might not be the desired output. We want 10.555
to become 10.56
, 10.5
to remain 10.5
, and 10
to remain 10
.
flowchart TD A[Input Number] --> B{Is `toFixed(2)` sufficient?} B -->|No, need to remove trailing zeros| C[Convert to String] C --> D[Parse as Float] D --> E[Output Rounded Number] B -->|Yes, already desired format| E E --> F[End]
Decision flow for rounding to at most two decimal places
Method 1: Using Math.round()
with Multiplication and Division
This is a classic and reliable method for precise rounding. By multiplying the number by a power of 10 (e.g., 100 for two decimal places), rounding it, and then dividing by the same power of 10, you can control the precision. The key is then to convert the result to a string and back to a number to remove unnecessary trailing zeros.
function roundToTwoDecimalPlaces(num) {
const rounded = Math.round(num * 100) / 100;
// Convert to string and then back to number to remove trailing zeros
return parseFloat(rounded.toString());
}
console.log(roundToTwoDecimalPlaces(10.555)); // Output: 10.56
console.log(roundToTwoDecimalPlaces(10.5)); // Output: 10.5
console.log(roundToTwoDecimalPlaces(10)); // Output: 10
console.log(roundToTwoDecimalPlaces(0.1 + 0.2)); // Output: 0.3 (handles floating point inaccuracies better than toFixed directly)
console.log(roundToTwoDecimalPlaces(123.4567)); // Output: 123.46
Rounding using Math.round() and parseFloat()
toFixed()
can sometimes exhibit unexpected behavior due to its internal string conversion and rounding rules, especially with numbers like 1.005
.Method 2: Using toFixed()
and parseFloat()
The toFixed()
method formats a number using fixed-point notation, returning a string. While it rounds to the specified number of decimal places, it always includes that many decimal places, padding with zeros if necessary. To get rid of these trailing zeros, we can parse the resulting string back into a floating-point number using parseFloat()
.
function roundToTwoDecimalPlacesFixed(num) {
return parseFloat(num.toFixed(2));
}
console.log(roundToTwoDecimalPlacesFixed(10.555)); // Output: 10.56
console.log(roundToTwoDecimalPlacesFixed(10.5)); // Output: 10.5
console.log(roundToTwoDecimalPlacesFixed(10)); // Output: 10
console.log(roundToTwoDecimalPlacesFixed(0.1 + 0.2)); // Output: 0.3 (can be 0.30 for some inputs due to toFixed's internal rounding)
console.log(roundToTwoDecimalPlacesFixed(1.005)); // Output: 1.01 (toFixed(2) rounds 1.005 to 1.01)
Rounding using toFixed() and parseFloat()
toFixed()
returns a string, and its rounding behavior can sometimes be unexpected with certain floating-point numbers (e.g., 1.005
might round to 1.00
in some environments or 1.01
in others depending on the implementation's tie-breaking rules). Always test thoroughly for critical applications.Method 3: Using a Regular Expression (for string manipulation)
If you're dealing with numbers that are already strings or you prefer a string-based approach after initial rounding, a regular expression can be used to trim trailing zeros and decimal points. This method is less about numerical rounding and more about formatting the string representation.
function roundAndTrim(num) {
let str = num.toFixed(2); // First, round to 2 decimal places (as a string)
str = str.replace(/\.0+$/, ''); // Remove trailing .00, .0
str = str.replace(/(\.\d*?)0+$/, '$1'); // Remove trailing zeros after decimal point
return parseFloat(str);
}
console.log(roundAndTrim(10.555)); // Output: 10.56
console.log(roundAndTrim(10.5)); // Output: 10.5
console.log(roundAndTrim(10)); // Output: 10
console.log(roundAndTrim(123.400)); // Output: 123.4
console.log(roundAndTrim(123.000)); // Output: 123
Rounding and trimming with regular expressions
toFixed()
for the initial rounding, inheriting its potential floating-point quirks. It's best used when you need fine-grained control over the string output after a preliminary rounding step.