Math.round()
Math.round(x) Math.round() rounds a number to the nearest integer, with .5 values rounding away from zero. This is the standard “arithmetic rounding” behavior you learned in school.
Syntax
Math.round(x)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | number | — | The number to round |
Return Value
The integer nearest to x. When exactly halfway between two integers (.5 cases), it rounds away from zero.
Examples
Basic usage
Math.round(4.7);
// 5
Math.round(4.4);
// 4
Math.round(4.5);
// 5
Math.round(-4.5);
// -4 (rounds UP toward zero)
Rounding to decimal places
Rounding to the nearest integer is useful, but most real-world numbers have cents, percentage points, or display precision to worry about. A common need is rounding to specific decimal places — for currency, percentages, or display values. Use multiplication and division to scale the number, apply Math.round(), then scale back:
// Round to 2 decimal places
function roundToFixed(number, decimals) {
const factor = 10 ** decimals;
return Math.round(number * factor) / factor;
}
roundToFixed(3.14159, 2);
// 3.14
roundToFixed(2.71828, 3);
// 2.718
roundToFixed(99.456, 1);
// 99.5
Negative numbers: the .5 behavior
The most counterintuitive aspect of Math.round() shows up with negative numbers. At exactly .5, the function rounds away from zero — so -2.5 rounds up to -2, not down to -3. This catches developers who assume “round up” means “toward positive infinity”:
// Positive: .5 rounds up (away from zero)
Math.round(2.5); // 3
Math.round(2.51); // 3
Math.round(2.49); // 2
// Negative: .5 rounds UP (toward zero!)
Math.round(-2.5); // -2
Math.round(-2.51); // -3
Math.round(-2.49); // -2
// This differs from Math.floor() and Math.ceil()
Math.floor(-2.5); // -3 (rounds down, away from zero)
Math.ceil(-2.5); // -2 (rounds up, toward zero)
This “round half away from zero” behavior matches most rounding conventions but can surprise developers expecting “round half to even” (banker’s rounding). The distinction matters most when rounding intermediate results — a small bias can compound. For display and formatting, however, the built-in behavior is exactly what you need.
Common formatting pattern: currency
A practical use case is formatting currency values:
function formatPrice(amount) {
// Round to 2 decimal places for cents
const cents = Math.round(amount * 100) / 100;
return cents.toFixed(2);
}
formatPrice(19.999);
// "20.00"
formatPrice(19.991);
// "19.99"
formatPrice(19.995);
// "20.00"
When rounding decisions matter
Math.round() is the method to reach for when you want the nearest integer in everyday user-facing math. That makes it common in cart totals, display counts, and any interface where a value should look like a whole number instead of a measurement. The behavior is simple, but the moment you apply it can change the result, especially if you round too early in a chain of calculations.
If accuracy matters, keep the value as a number until the last possible step. That way you avoid rounding a partial result and then rounding again after more arithmetic. In reporting code, this small discipline can prevent totals from drifting by a cent or two.
Picking the right rounding helper
Math.round() is only one part of the rounding family. Use Math.floor() when you need to stay at or below the input, Math.ceil() when you need to stay at or above it, and Math.trunc() when you only want to remove the fractional part. Each one answers a different question, so the best choice depends on what the number means in your domain.
For negative values, the differences become easier to see. That is why tests around boundary cases are useful: they show whether your code is rounding for display, for storage, or for a rule that depends on strict numeric thresholds.
round vs floor vs ceil vs trunc
| Expression | 3.7 | -3.7 | 3.5 | -3.5 |
|---|---|---|---|---|
Math.round(x) | 4 | -4 | 4 | -4 |
Math.floor(x) | 3 | -4 | 3 | -4 |
Math.ceil(x) | 4 | -3 | 4 | -3 |
Math.trunc(x) | 3 | -3 | 3 | -3 |
Key differences:
- round: Nearest integer (.5 rounds away from zero)
- floor: Always rounds toward negative infinity
- ceil: Always rounds toward positive infinity
- trunc: Simply removes the fractional part, toward zero
Edge Cases
Math.round(Infinity); // Infinity
Math.round(-Infinity); // -Infinity
Math.round(NaN); // NaN
Math.round(0); // 0
Math.round(-0); // -0
Math.round(0.5); // 1
Math.round(-0.5); // -0 (rounds toward zero)
Performance Note
Math.round() is fast on all modern engines. For maximum performance in tight loops, the bitwise trick ~~x or x + 0.5 | 0 can work, but readability suffers. Prefer Math.round().
See Also
- Math.floor() — Round down
- Math.ceil() — Round up
- Math.trunc() — Truncate decimal
- Number.isInteger() — Check if a value is an integer