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.

4 min read

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?"

javascriptjavascript
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.

javascriptjavascript
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:

javascriptjavascript
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.

javascriptjavascript
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:

javascriptjavascript
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.

javascriptjavascript
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.

javascriptjavascript
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

Method3.23.8-3.2-3.8Direction
Math.floor33-4-4Toward -infinity
Math.ceil44-3-3Toward +infinity
Math.round34-3-4Nearest (0.5 up)
Math.trunc33-3-3Toward zero

Rounding to decimal places

Math methods produce integers. To round to a specific number of decimal places, use toFixed:

javascriptjavascript
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

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.
RunePowered by Rune AI

Frequently Asked Questions

What is the difference between Math.round and Math.floor?

Math.round rounds to the nearest integer, with .5 rounding up. Math.floor always rounds down. Math.round(3.2) is 3 and Math.round(3.7) is 4, while Math.floor always gives 3 for both.

How does Math.floor handle negative numbers?

Math.floor rounds down, which on the number line means more negative. Math.floor(-2.3) is -3, not -2. This surprises many beginners coming from other languages.

Does JavaScript have a truncation method?

Yes. Math.trunc removes the fractional part without rounding: Math.trunc(3.9) is 3 and Math.trunc(-3.9) is -3. It simply cuts off everything after the decimal point.

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.