Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. Math.floor() / ceil() / round() / trunc()

Math.floor() / ceil() / round() / trunc()

Methods for converting a number to an integer by handling its fractional part. There are four types: floor (round down), ceiling (round up), rounding, and truncation.

Syntax

// Rounds down to the nearest integer.
var num = Math.floor(number);

// Rounds up to the nearest integer.
var num = Math.ceil(number);

// Rounds to the nearest integer.
var num = Math.round(number);

// Removes the fractional part without rounding.
var num = Math.trunc(number);

Method List

MethodDescription
Math.floor(number)Returns the largest integer less than or equal to the given number. For positive numbers, it rounds down; for negative numbers, it moves to a smaller integer.
Math.ceil(number)Returns the smallest integer greater than or equal to the given number. For positive numbers, it rounds up; for negative numbers, it moves to a larger integer.
Math.round(number)Returns the integer nearest to the given number. If the fractional part is exactly 0.5, it rounds up.
Math.trunc(number)Returns the integer part of a number by simply removing the fractional digits. For positive numbers, the result is the same as floor; for negative numbers, the same as ceil.

Sample Code

// Comparison with positive numbers
console.log(Math.floor(3.7)); // Outputs '3'. Rounds down.
console.log(Math.ceil(3.2)); // Outputs '4'. Rounds up.
console.log(Math.round(3.5)); // Outputs '4'. Rounds to nearest.
console.log(Math.trunc(3.7)); // Outputs '3'. Removes the fractional part.

// Behavior differs with negative numbers.
console.log(Math.floor(-3.2)); // Outputs '-4'. Moves to the smaller integer.
console.log(Math.ceil(-3.7)); // Outputs '-3'. Moves to the larger integer.
console.log(Math.round(-3.5)); // Outputs '-3'.
console.log(Math.trunc(-3.7)); // Outputs '-3'. Only the fractional part is removed.

// Practical example: generate a random integer from 0 to 9
var random = Math.floor(Math.random() * 10);
console.log(random); // Outputs one of the integers from 0 to 9.

// Practical example: calculate the number of pages
var totalItems = 23;
var itemsPerPage = 10;
var totalPages = Math.ceil(totalItems / itemsPerPage);
console.log(totalPages); // Outputs '3'. Displaying 23 items at 10 per page requires 3 pages.

Overview

All four methods convert a number to an integer, but they differ in how they handle the fractional part. The most commonly used is Math.floor(), and the pattern of combining it with Math.random() to generate a random integer is a classic technique.

When working with positive numbers only, Math.floor() and Math.trunc() produce the same result. However, with negative numbers they differ: Math.floor(-3.2) returns -4, while Math.trunc(-3.2) returns -3. When you simply want to strip the decimal part, Math.trunc() is more intuitive.

Math.ceil() is commonly used for pagination calculations. To display 23 items at 10 per page, Math.ceil(23 / 10) gives you the required number of pages: 3.

Browser Compatibility

Chrome Chrome
49+
Supported in all versions
Firefox Firefox
57+
Supported in all versions
Safari Safari
18+
Supported in all versions
Edge Edge
80+
11 or earlier ×
IE IE
11+
2 or earlier ×
Opera Opera
48+
2 or earlier ×
iOS Safari iOS Safari
18+
Supported in all versions
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .