JavaScript Rounding Numbers Floor Ceil and Round
Compare JavaScript Math.floor, Math.ceil, and Math.round for rounding numbers. Learn the key differences, negative number behavior, and when to use each.
JavaScript provides three Math methods for converting decimals to integers: floor, ceil, and round. Each answers a different question. Floor asks "what is the nearest integer at or below?" Ceil asks "what is the nearest integer at or above?" Round asks "what is the nearest integer?"
console.log(Math.floor(3.7));
console.log(Math.ceil(3.7));
console.log(Math.round(3.7));Floor prints 3 because it always goes down. Ceil prints 4 because it always goes up. Round prints 4 because 3.7 is closer to 4 than to 3.
For an overview of all Math methods, see the JavaScript Math object guide.
Math.floor -- always round down
Math.floor returns the largest integer less than or equal to the given number. With positive numbers, it simply drops the decimal part.
console.log(Math.floor(5.1));
console.log(Math.floor(5.9));
console.log(Math.floor(5.0));The first prints 5, the second prints 5, the third prints 5. Floor treats 5.1 and 5.9 identically because both are between 5 and 6.
With negative numbers, "down" means more negative on the number line:
console.log(Math.floor(-2.1));
console.log(Math.floor(-2.9));The first prints -3, the second prints -3. This surprises many beginners who expect floor to strip the decimal sign like truncation would. Floor moves toward negative infinity, not toward zero.
Floor is the method you combine with Math.random for random integer generation. See the guide to random number generation for that pattern.
Math.ceil -- always round up
Math.ceil returns the smallest integer greater than or equal to the given number. It is the mirror of floor.
console.log(Math.ceil(5.1));
console.log(Math.ceil(5.9));
console.log(Math.ceil(-2.1));The first prints 6, the second prints 6, the third prints -2. With negative numbers, "up" means toward zero, which can feel counterintuitive alongside floor's behavior.
Ceil is useful when you need to ensure a value is never below a threshold, such as calculating how many pages are needed for a list of items:
const itemsPerPage = 10;
const totalItems = 27;
const pages = Math.ceil(totalItems / itemsPerPage);
console.log(pages);This prints 3 because 27 items at 10 per page requires 3 pages. Even the partially full last page counts as a whole page.
Math.round -- nearest integer
Math.round returns the integer closest to the given number. When exactly halfway (.5), it rounds up.
console.log(Math.round(5.3));
console.log(Math.round(5.7));
console.log(Math.round(5.5));
console.log(Math.round(-2.5));The first prints 5, the second prints 6, the third prints 6 because .5 rounds up. The fourth prints -2 because -2.5 rounds up (toward zero) to -2 rather than down to -3.
Round is the most intuitive method for general-purpose rounding, but the .5-up tie-breaking rule is important to remember when processing financial data where .5 should sometimes round to even.
Math.trunc -- just drop the decimal
Math.trunc simply removes the fractional part without any rounding logic. It acts like chopping off everything after the decimal point.
console.log(Math.trunc(5.9));
console.log(Math.trunc(-5.9));Both print 5 and -5 respectively. Trunc always moves toward zero, unlike floor which moves toward negative infinity.
Comparison table
| Method | 3.2 | 3.8 | -3.2 | -3.8 | Direction |
|---|---|---|---|---|---|
| Math.floor | 3 | 3 | -4 | -4 | Toward -infinity |
| Math.ceil | 4 | 4 | -3 | -3 | Toward +infinity |
| Math.round | 3 | 4 | -3 | -4 | Nearest (0.5 up) |
| Math.trunc | 3 | 3 | -3 | -3 | Toward zero |
Rounding to decimal places
Math methods produce integers. To round to a specific number of decimal places, use toFixed:
const price = 59.976;
console.log(price.toFixed(2));
console.log(price.toFixed(0));The first prints "59.98", rounded to two decimal places. The second prints "60". toFixed returns a string, not a number. Convert back with Number() when further math is needed.
Common mistakes
Expecting floor to behave like truncation for negative numbers is the most frequent error. Math.floor(-2.1) returns -3, not -2. Use Math.trunc if you want to simply drop the decimal.
Using round for display rounding instead of toFixed means you cannot control decimal places. Math.round(59.976) returns 60, and you lose the cents entirely. For currency and display, toFixed is usually the better tool.
Assuming ceil on a negative number rounds it "more negative" is incorrect. Math.ceil(-2.1) returns -2 because -2 is greater than (above) -2.1 on the number line.
Rune AI
Key Insights
- Math.floor always rounds down. Math.floor(-2.3) is -3.
- Math.ceil always rounds up. Math.ceil(-2.3) is -2.
- Math.round rounds to the nearest integer, with .5 rounding up.
- Math.trunc simply removes the fractional part, like chopping off the decimal.
- For fixed decimal places, use Number.toFixed(n) instead of Math methods.
Frequently Asked Questions
What is the difference between Math.round and Math.floor?
How does Math.floor handle negative numbers?
Does JavaScript have a truncation method?
Conclusion
Math.floor always rounds down, Math.ceil always rounds up, and Math.round rounds to the nearest integer. The critical difference is how they handle negative numbers: floor goes more negative, ceil goes less negative, and round follows the .5-up rule. For display rounding with decimal places, use toFixed instead.
More in this topic
Is JavaScript Frontend or Backend? Full Guide
JavaScript is both a frontend and backend language. Learn the difference between browser JavaScript and Node.js, what each is used for, and which one you should learn first.
Learn JavaScript Step by Step Tutorial with Real Examples
Follow a hands-on tutorial that teaches JavaScript by building a real interactive page. Write your first variables, functions, and event listeners with examples you can run.
JavaScript Tutorial: Complete Beginner's Guide to Programming in 2025
A structured learning path for absolute beginners. Learn what to study first, how to practice, and which JavaScript concepts matter most when you are starting from zero.