Ad space (banner)
🟨JavaScript Lessons
Lesson 46 / 69

Rounding Numbers (floor, ceil, and round)

In this lesson you'll learn the differences between the three number-rounding methods Math.floor, ceil, and round, so you can correctly choose the right one for your purpose. This is for people searching "JavaScript round down, round up, and round to nearest."

Math.floor() rounds down, Math.ceil() rounds up, and Math.round() rounds to the nearest integer. Picture them as "shave it down," "bump it up," and "round to whichever is closer." All three convert a number that may include a decimal into an integer, but you need to correctly understand that they produce different results.

The sample code confirms that Math.floor() on 3.7 gives 3, Math.ceil() on 3.2 gives 4, and Math.round() on 3.5 gives 4. Try it with different input values to also check which way a boundary value (exactly .5, for instance) gets rounded.

A common beginner stumbling block is how these behave with negative numbers. Math.floor(-3.5) gives -4, a result that can feel different from your intuition with positive numbers. Converting a decimal into an integer comes up extremely often — for money calculations, computing a page count, and more — so correctly choosing which one to use matters in real development too.

Which rounding method you choose changes the result, so choosing correctly based on your purpose matters a lot. Accidentally rounding up in a money calculation can create a mismatch between the actual charge and the displayed amount — a real practical problem — so the choice of rounding method must be made carefully.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)