Ship internal tools in hours, not weeks. Real auth, users, jobs, audit logs, and cohesive UI included. Early access $249 $499 → [Get it now]

How to round a number in JavaScript

Rounding numbers is essential for displaying prices, calculating percentages, formatting measurements, and data visualization in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve implemented number rounding in pricing displays, progress indicators, and chart labels where precise formatting matters. JavaScript provides four rounding functions — Math.round(), Math.floor(), Math.ceil(), and Math.trunc() — each with distinct behavior, especially for negative numbers.

Use Math.round() to round to the nearest integer using standard mathematical rounding.

Math.round(3.5)   // 4
Math.round(3.4)   // 3
Math.round(-1.5)  // -1  — rounds toward +∞ at midpoints

1. Math.round()

Math.round() rounds to the nearest integer. At exactly .5, it rounds toward positive infinity — meaning -1.5 rounds to -1, not -2. This surprises many developers.

Math.round(4.7)    // 5
Math.round(4.5)    // 5
Math.round(4.4)    // 4
Math.round(-4.5)   // -4  — not -5
Math.round(-4.6)   // -5

2. Math.floor()

Math.floor() always rounds down (toward negative infinity), regardless of the decimal.

Math.floor(4.9)   // 4
Math.floor(4.1)   // 4
Math.floor(-4.1)  // -5  — rounds away from zero for negatives
Math.floor(-4.9)  // -5

Use Math.floor() when you need the largest integer less than or equal to the value — common for array index calculations and page counts.

3. Math.ceil()

Math.ceil() always rounds up (toward positive infinity).

Math.ceil(4.1)   // 5
Math.ceil(4.9)   // 5
Math.ceil(-4.9)  // -4  — rounds toward zero for negatives
Math.ceil(-4.1)  // -4

Use Math.ceil() for “how many pages do I need?” or “reserve at least N slots” type calculations.

4. Math.trunc()

Math.trunc() removes the decimal portion entirely — it always rounds toward zero, unlike Math.floor() which rounds toward negative infinity.

Math.trunc(4.9)   // 4
Math.trunc(-4.9)  // -4  — toward zero, not -5
Math.floor(-4.9)  // -5  — toward -∞

Math.trunc(0.9)   // 0
Math.trunc(-0.9)  // -0  (negative zero, rarely matters in practice)

For positive numbers Math.trunc() and Math.floor() are equivalent. For negative numbers they differ — choose based on intent.

5. Rounding to Decimal Places

To round to N decimal places, multiply by 10^N, round, then divide.

Math.round(3.7456 * 100) / 100   // 3.75
Math.round(3.7456 * 10) / 10     // 3.7
Math.round(3.7456 * 1000) / 1000 // 3.746

Floating-point caveat: IEEE 754 arithmetic means some values round incorrectly with this approach. For example Math.round(1.005 * 100) / 100 returns 1 instead of 1.01. Add Number.EPSILON before rounding to correct for this:

function roundTo(value, decimals) {
  const factor = Math.pow(10, decimals)
  return Math.round((value + Number.EPSILON) * factor) / factor
}

roundTo(1.005, 2)   // 1.01  ✓
roundTo(3.7456, 2)  // 3.75
roundTo(1.255, 2)   // 1.26

This is the reliable pattern to use whenever displaying financial or scientific values.

6. toFixed() for String Formatting

toFixed(n) rounds to n decimal places and returns a string — useful for display but not for further arithmetic.

(3.7456).toFixed(2)   // '3.75'
(1.005).toFixed(2)    // '1.00' — same IEEE 754 caveat applies
(100).toFixed(2)      // '100.00' — pads with zeros

// Convert back to number if needed
parseFloat((3.7456).toFixed(2))  // 3.75

For locale-aware currency formatting, prefer Intl.NumberFormat over toFixed():

new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(3.7456)
// '$3.75'

7. Choosing the Right Method

Method Direction Negative -4.5 Use case
Math.round() Nearest (ties → +∞) -4 General rounding
Math.floor() Down (→ -∞) -5 Index calc, page counts
Math.ceil() Up (→ +∞) -4 Allocation, pagination
Math.trunc() Toward zero -4 Drop decimal, integer part
toFixed(n) Nearest (string) '-4.50' Display formatting

When working with random numbers that need rounding, see how to generate a random number in JavaScript.

Best Practice Note:

Use roundTo(value, decimals) with Number.EPSILON whenever precision matters — the plain multiply-round-divide pattern silently fails for values like 1.005. For UI display of currency or percentages, Intl.NumberFormat handles locale, grouping separators, and currency symbols automatically and is more robust than manual toFixed() concatenation. We apply these patterns in CoreUI’s chart labels and progress bar percentage displays to avoid off-by-one rounding errors in dashboard UIs.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author