Emmanuel Akinfulubi

JavaScriptMath

Round a decimal to an integer in JavaScript

Beginner · 3 min read

Real numbers are messy. A rating comes out as 4.7 stars, a page count as 2.4 pages, a bus trip needs 2.1 buses. Sooner or later you need a whole number, and JavaScript gives you four different ways to get one.

In this tutorial you will run Math.floor, Math.ceil, Math.round and Math.trunc on four positive numbers, 2.7, 2.4, 2.5 and 2.1, and see where they agree and where they don't. You need to know how to call a function and print the result.

The four functions

Each one turns a decimal into a whole number, but each has its own rule:

  • Math.floor goes down, to the next whole number below.
  • Math.ceil (short for "ceiling") goes up, to the next whole number above.
  • Math.round goes to the nearest whole number.
  • Math.trunc (short for "truncate") chops off everything after the decimal point.

A picture helps. Imagine the number line: 2 on the left, 3 on the right, 2.7 in between. Floor moves left to 2. Ceil moves right to 3. Round picks whichever is closer, 3. Trunc just deletes the .7 and leaves 2.

Here is the exercise. Try it yourself first if you like, then read on and we will go through it together.

5 floor, ceil, round and trunc on a positive number

easy

Turn 2.7, 2.4, 2.5 and 2.1 into integers with all four rounding functions, and say which two agree on every positive input.

Stuck? Three hints, smallest first
  1. 1. floor goes towards minus infinity, ceil towards plus infinity.
  2. 2. trunc discards the fraction, which on a positive number is the same as floor.
  3. 3. round goes to the nearest, and a tie goes up.

Step 1: one number, four answers

Start with 2.7:

Step 1

console.log(Math.floor(2.7)); // 2
console.log(Math.ceil(2.7));  // 3
console.log(Math.round(2.7)); // 3
console.log(Math.trunc(2.7)); // 2

Floor and trunc agree on 2. Ceil and round agree on 3.

Step 2: a helper to compare them

Writing four lines per number gets tiring. A small helper returns all four results as an object, so one call shows everything:

Step 2

const show = (n) => ({
  floor: Math.floor(n),
  ceil: Math.ceil(n),
  round: Math.round(n),
  trunc: Math.trunc(n),
});
console.log(show(2.4)); // { floor: 2, ceil: 3, round: 2, trunc: 2 }
console.log(show(2.1)); // { floor: 2, ceil: 3, round: 2, trunc: 2 }

The brackets around the curly braces, ({ ... }), tell JavaScript the arrow function returns an object. Without them, the braces would be read as the body of the function.

Look at 2.1: Math.ceil still goes up to 3, even though 2.1 is barely past 2. Ceil doesn't care how far past; any fraction at all moves it up.

Step 3: the halfway case

What should 2.5 do? It is exactly between 2 and 3.

Step 3

const show = (n) => ({
  floor: Math.floor(n),
  ceil: Math.ceil(n),
  round: Math.round(n),
  trunc: Math.trunc(n),
});
console.log(show(2.5)); // { floor: 2, ceil: 3, round: 3, trunc: 2 }

Math.round breaks a tie by going up, so 2.5 becomes 3. Floor, ceil and trunc ignore the tie: they follow their own rules as always.

Step 4: which two always agree?

Across every example, Math.floor and Math.trunc gave the same answer. On a positive number that is always true: chopping off the fraction and going down to the next whole number are the same move.

Step 4

console.log([2.1, 2.5, 2.9].every((n) => Math.trunc(n) === Math.floor(n))); // true

every returns true only if the check passes for every item in the array. Keep this agreement in mind: it breaks as soon as the numbers go negative, which is the next exercise.

Step 5: whole numbers are left alone

Step 5

console.log(Math.ceil(3));  // 3
console.log(Math.floor(3)); // 3

A number that is already whole doesn't move. That matters more than it seems. Say 60 passengers need buses that seat 30. Math.ceil(60 / 30) must be 2, not 3. If ceil always added one, you would book an empty bus.

JavaScript

console.log(Math.ceil(60 / 30)); // 2
console.log(Math.ceil(61 / 30)); // 3

The full solution

The answer panel uses the same show helper on 2.7, 2.4, 2.5, 2.1 and 3. The checks underneath confirm each rule, that trunc and floor agree on positives, and that each function returns a number, not text. Press Run to see the output.

The answer

const show = (n) => ({
  floor: Math.floor(n),
  ceil: Math.ceil(n),
  round: Math.round(n),
  trunc: Math.trunc(n),
});

console.log('2.7 ->', show(2.7));
console.log('2.4 ->', show(2.4));
console.log('2.5 ->', show(2.5), '<- a tie goes up');
console.log('2.1 ->', show(2.1));
console.log('3   ->', show(3), '<- a whole number is untouched by all four');

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('floor goes down', Math.floor(2.7), 2);
test('ceil goes up, even for 2.1', Math.ceil(2.1), 3);
test('round takes the nearest', [Math.round(2.4), Math.round(2.6)], [2, 3]);
test('and a tie rounds up', Math.round(2.5), 3);
test('trunc throws the fraction away', Math.trunc(2.7), 2);
test('on positives, trunc and floor always agree', [2.1, 2.5, 2.9].every((n) => Math.trunc(n) === Math.floor(n)), true);
test('all four leave a whole number alone', show(3), { floor: 3, ceil: 3, round: 3, trunc: 3 });
test('ceil of a whole number does not add one', Math.ceil(3), 3);
test('and each returns a number, not a string', typeof Math.round(2.5), 'number');

On positive numbers only ceil stands apart, so it is easy to believe the four are near-interchangeable. They are not, and exercise 6 is the proof. Pick by intent: floor for a page index, ceil for "how many buses do I need", round for a display value, trunc for "drop the fraction".

Math.ceil(3) being 3 matters for the bus question: Math.ceil(passengers / seats) must not add a phantom vehicle when the division comes out even.

Common mistakes

  • Using Math.round when you need a count. "How many boxes for 21 items, 10 per box?" needs Math.ceil(21 / 10), which is 3. Math.round gives 2 and leaves an item behind.
  • Thinking floor and trunc are the same function. They only agree on positive numbers. On negatives they split apart.
  • Forgetting the brackets in ({ ... }). Without them, an arrow function that should return an object returns undefined.

Try it yourself

  1. A blog shows 10 posts per page. Use the right function to work out how many pages 47 posts need.
  2. Run show(2.49) and show(2.51). Which function changes its answer between the two?
  3. Turn a rating like 4.7 into a whole number of stars, rounded to the nearest. Then try 4.5.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.