HOWTO · JavaScript
How to Round a Number to 2 Decimal Places in JavaScript
Compare JavaScript number formatting and numeric rounding to two decimal places, including their output types and floating-point limitations.
On this page
Use toFixed(2) when you need text that always shows two digits after the decimal point. It returns a string, so trailing zeroes stay visible. For a numeric result, use Math.round(value * 100) / 100 or convert the formatted string with Number(). These approaches are convenient for ordinary values, but JavaScript stores numbers in binary floating-point, so decimal inputs such as 1.005 may not round as a reader expects.
Format a number with toFixed()
Call toFixed(2) on a number to get a string with two fractional digits. This is useful for a label, table cell, or other display where 5.60 should retain its final zero. The returned value is text, however; arithmetic with it requires converting it back to a number.
Run this example:
const value = 5.6789;
const fixedText = value.toFixed(2);
const roundedValue = Math.round(value * 100) / 100;
const numericValue = Number(value.toFixed(2));
function roundToTwo(number) {
const shifted = Number(`${number}e2`);
return Number(`${Math.round(shifted)}e-2`);
}
const shiftedValue = roundToTwo(value);
console.log(fixedText, roundedValue, numericValue, shiftedValue);
5.68 5.68 5.68 5.68
In order, the values are the toFixed() string, the Math.round() number, the Number() conversion, and the decimal-shift helper result. Only the first value is text; converting it with Number() produces a number and does not preserve trailing zeroes.
Round a number with Math.round()
Multiplying by 100 moves the hundredths place into the units position. Math.round() rounds that scaled value to an integer, and dividing by 100 restores the decimal scale. The result remains a number, which is convenient for later arithmetic but does not encode how many decimal digits to display.
Math.round() resolves half-way values toward positive infinity. For example, a tie at -1.5 rounds to -1, so negative values do not follow a rule that always rounds away from zero. Also, multiplication itself uses floating-point arithmetic; the pattern is concise, not a guarantee of exact decimal rounding for every input.
Understand floating-point edge cases
JavaScript’s Number type represents values using binary floating-point. Many decimal fractions cannot be represented exactly in binary, and intermediate operations can land just below or above the decimal value a person intended. A familiar case is 1.005: its stored value is slightly below the decimal fraction, so 1.005.toFixed(2) can produce "1.00", and scaling before Math.round() can also give an unexpected result.
Number.EPSILON is the gap between 1 and the next representable number. Adding it before rounding can help with some inputs near 1, but the gap between representable numbers changes with magnitude. A fixed epsilon adjustment therefore does not repair all values or all arithmetic histories.
toPrecision() controls significant digits rather than digits after the decimal point. For example, Number((1.23456).toPrecision(4)).toFixed(2) produces "1.24", while (1.23456).toFixed(2) produces "1.23": the first operation rounds to 1.235, and the second rounds that intermediate value again. This is one particular double-rounding case, not a general-purpose correction; the first formatting step can discard information needed by the later step.
Use a decimal-shift helper
The helper above shifts the decimal scale by using exponent notation, rounds the shifted value, and shifts it back. This can avoid some surprises caused by directly multiplying a decimal fraction by 100, while still returning a number. Adapt the exponent when rounding to another number of places: use that count for the forward shift and its negative for the return shift.
Treat this helper as a practical option for ordinary finite values, not as a decimal-arithmetic library. Floating-point representation still applies, and extreme magnitudes or special values can need separate handling. If exact decimal behavior is required for money or accounting, use integer minor units when the domain permits it, or a decimal arithmetic library with rules chosen for the application. Formatting a result for display and performing exact decimal calculations are separate requirements.
Choose the right approach
Choose toFixed(2) when the goal is fixed-width display text; it keeps trailing zeroes and returns a string. Choose Math.round(value * 100) / 100 when a compact numeric result is useful and the input range is suitable for ordinary floating-point arithmetic. Choose Number(value.toFixed(2)) when you want the toFixed() rounding behavior followed by a numeric value, knowing that numeric values do not retain display formatting. Consider the decimal-shift helper as another numeric option, with its limits understood.
None of these methods makes JavaScript numbers exact decimal quantities. For interfaces, format at the point of display. For calculations with strict decimal rules, define the rounding policy and numeric representation explicitly, then test representative values from the application’s domain.