How to Round Numbers in JavaScript
Use JavaScript rounding methods when you need cleaner prices, percentages, UI counters, chart labels, or decimal formatting. The key is choosing the right method for up, down, or nearest-value behavior.
What you’ll build or solve
You’ll learn how to round numbers in JavaScript using Math.round(), Math.floor(), and Math.ceil(). You’ll also know how to round decimal places.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when numbers must be displayed or calculated as cleaner whole values.
Common real-world scenarios include:
- Currency display
- Progress percentages
- Rating averages
- Chart labels
- Pagination math
This is a bad idea when you need exact financial precision across many decimal operations. In that case, use decimal-safe libraries or integer cents.
Prerequisites
You only need:
- A JavaScript file or browser console
- Basic math knowledge
Step-by-step instructions
Step 1: Pick the right rounding method
Use Math.round() for the nearest whole number.
JavaScript
const rating = 4.6;
const roundedRating = Math.round(rating);
console.log(roundedRating);
Use Math.floor() to always round down.
JavaScript
const pages = Math.floor(4.9);
Use Math.ceil() to always round up.
JavaScript
const totalPages = Math.ceil(4.1);
For decimal places, scale first, round, then scale back.
JavaScript
const price = 19.987;
const roundedPrice = Math.round(price * 100) / 100;
What to look for:
round()goes to the nearest whole numberfloor()always rounds downceil()always rounds up- Multiply before rounding decimals
- Divide back after rounding
Examples you can copy
Star rating
JavaScript
const stars = Math.round(4.4);
Total pages
JavaScript
const pages = Math.ceil(totalItems / perPage);
Two decimal price
JavaScript
const price = Math.round(19.987 * 100) / 100;
Common mistakes and how to fix them
Mistake 1: Using Math.floor() when nearest rounding is needed
What the reader might do:
JavaScript
Math.floor(4.9);
Why it breaks: this always rounds down.
Corrected approach:
JavaScript
Math.round(4.9);
Mistake 2: Forgetting to divide after decimal scaling
What the reader might do:
JavaScript
Math.round(price * 100);
Why it breaks: the value stays scaled as cents.
Corrected approach:
JavaScript
Math.round(price * 100) / 100;
Mistake 3: Using string formatting for math logic
What the reader might do:
JavaScript
const rounded = price.toFixed(2);
Why it breaks: toFixed() returns a string.
Corrected approach:
Use math rounding when the value stays numeric.
Troubleshooting
If decimals remain, confirm the divide-back step exists.
If the number always rounds down, switch from floor() to round().
If pagination undercounts, use ceil().
If the result becomes a string, avoid toFixed() for math operations.
Quick recap
- Use
Math.round()for nearest value - Use
floor()for down - Use
ceil()for up - Multiply and divide for decimals
- Keep numbers numeric for math logic
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot