Math.round()
Rounds a number to the nearest integer, breaking ties toward positive infinity.
Math.round(x)- Added in JavaScript ES1
Parameters
| Parameter | Type | Description |
|---|---|---|
$x | number | A number to round. Non-numbers are coerced first. |
Return value
numberThe value of x rounded to the nearest integer. A fractional part of exactly 0.5 rounds toward +Infinity.
Math.round() returns the nearest integer. The rule that catches people is the tie-break: a fractional part of exactly .5 rounds toward positive infinity, not away from zero. So Math.round(1.5) is 2 but Math.round(-1.5) is -1, and Math.round(-2.5) is -2. Most other languages round halves away from zero, so ported code and cross-language test fixtures disagree at exactly these values.
When to reach for it
Use it for display: percentages, scores, chart labels, anything a human reads where nearest is the honest answer. Use Math.floor() or Math.ceil() instead when a partial unit must consistently count as zero or one whole unit.
What trips people up
Rounding to N decimals via Math.round(n * 100) / 100 is not exact. 1.005 is stored as slightly less than 1.005, so 1.005 * 100 is 100.49999999999999 and the result rounds down to 1. This is the single most-reported "rounding bug" in JavaScript, and it is a property of binary floating point, not of Math.round(). For money, work in integer cents or format with Intl.NumberFormat.
Math.round() is also not equivalent to Math.floor(x + 0.5): they differ for -0 and for inputs in -0.5 ≤ x < 0.
Example
When to use it
- Rounding a computed percentage for display in a UI
- Snapping a drag position to the nearest whole pixel or grid cell
- Producing a nearest-integer score or rating from a weighted average
- Formatting a measurement to a fixed number of decimals for a label
Frequently asked questions
Why does Math.round(-1.5) return -1 instead of -2?
Because JavaScript breaks ties toward positive infinity rather than away from zero. Any exact .5 moves up the number line, so -1.5 goes to -1 and -2.5 goes to -2.
Why does Math.round(1.005 * 100) / 100 give 1 instead of 1.01?
Because 1.005 cannot be represented exactly in binary floating point — it is stored as slightly less, so 1.005 * 100 is 100.49999999999999 and rounds down. Use integer cents or a string-based helper for exact decimal rounding.
What is the difference between Math.round() and toFixed()?
Math.round() returns a number rounded to an integer, while toFixed(n) returns a string with n decimal places. toFixed also rounds using the number's exact binary value, so it can disagree with a multiply-round-divide approach on tie values.
More examples
Rounding halves away from zero instead
Taking the absolute value before rounding and reapplying the sign afterwards gives the away-from-zero behaviour most other languages use. The two columns only differ on exact halves; -1.5 becomes -1 under Math.round and -2 under halfUp.
const halfUp = (x) => Math.sign(x) * Math.round(Math.abs(x));
for (const n of [1.5, -1.5, 2.5, -2.5, -0.5]) {
console.log(n, "Math.round:", Math.round(n), "halfUp:", halfUp(n));
}A safer fixed-decimal rounding helper
Shifting the exponent through the number's decimal string avoids the multiply-by-100 error, so 1.005 rounds to 1.01 as expected. Note -1.005 still gives -1 because ties round toward +Infinity. toFixed is simpler but returns a string and has its own tie quirks.
function roundTo(value, digits) {
// Re-parse through the decimal string to sidestep binary error
return Number(Math.round(Number(`${value}e${digits}`)) + `e-${digits}`);
}
console.log(Math.round(1.005 * 100) / 100); // 1
console.log(roundTo(1.005, 2)); // 1.01
console.log(roundTo(2.675, 2)); // 2.68
console.log(roundTo(-1.005, 2)); // -1
console.log((1234.5678).toFixed(2)); // "1234.57" (a string)
Discussion