Course contentsShow
← JavaScript
Lesson 136 of 2,7234. Working with NumbersFree lesson

Rounding: Math.floor, Math.ceil, Math.round, Math.trunc

Different strategies for rounding numbers to integers and when to use each method.

Rounding: Math.floor, Math.ceil, Math.round, Math.trunc

You'll learn the four different ways JavaScript rounds numbers to integers, and when to use each one.

The Core Idea

When working with decimal numbers, you often need to convert them to whole numbers. JavaScript provides four methods, each with a different rounding strategy:

Math.floor – Always Round Down

Math.floor() rounds down to the nearest integer, like dropping to the floor below you. It moves toward negative infinity.

Math.floor(4.9);   // 4
Math.floor(4.1);   // 4
Math.floor(-4.1);  // -5 (down toward more negative)

Math.ceil – Always Round Up

Math.ceil() rounds up to the nearest integer, like reaching for the ceiling above you. It moves toward positive infinity.

Math.ceil(4.1);   // 5
Math.ceil(4.9);   // 5
Math.ceil(-4.9);  // -4 (up toward less negative)

Math.round – Round to Nearest

Math.round() rounds to the nearest integer. If the decimal is exactly .5, it rounds up.

Math.round(4.4);   // 4
Math.round(4.5);   // 5
Math.round(4.6);   // 5
Math.round(-4.5);  // -4 (rounds toward positive)

Math.trunc – Just Remove Decimals

Math.trunc() simply removes the decimal part, like truncating text. It always moves toward zero.

Math.trunc(4.9);   // 4
Math.trunc(-4.9);  // -4 (removes decimals, no rounding)

When to Use Each

  • Math.floor: When you need the largest integer less than or equal to a number
  • Math.ceil: When you need the smallest integer greater than or equal to a number (like calculating pages needed)
  • Math.round: When you want conventional "nearest integer" rounding
  • Math.trunc: When you want to discard decimals without any rounding

Key Takeaway: JavaScript offers four rounding methods—floor (down), ceil (up), round (nearest), and trunc (remove decimals)—each suited to different rounding needs.