Let's talk about Math.ceil, Math.floor, and Math.round πΏ

Search for a command to run...

No comments yet. Be the first to comment.
(This article was primarily written for another blog. The language of this article is more formal. Please let me know your thoughts through the comments.) Writing better and high-quality code is essential. Better code makes software stable and seamle...

This article is going to be a fun one. In this article, we'll be building a Tic Tac Toe game just by using HTML, CSS, and JavaScript. This is going to be an easy one. Both the inputs have to be placed by the user. But in another article, we will be c...

I am sure at some point in your life while browsing the web, you have found a web page that shows an animated counter. A counter that starts from 0 and goes all the way up to some given number. Here is an example of how such a counter looks. In this...

Building a JavaScript project always gives an excellent overview of the internals of the language. And also, it helps to make the portfolio better. In todayβs article, we will be building a fantastic mini JavaScript project. We aim to develop a Passw...

Building a Weather app with JavaScript is an excellent project for beginners. It helps to understand the core basics of the DOM and teaches how to use fetch API, to call and get data from a third-party service. In this tutorial, we'll be building a g...

We all have learned about rounding off numbers in our school. We usually increase the integer if its value is >=.5 and decrease it, if it is <= .4.
1.5 β 2
1.4 β 1
We all know JavaScript has a built-in object called Math, which has properties and methods for mathematical constants and functions.
We have three methods that are mostly used to round off a number in JS, i.e., Math.ceil(), Math.floor(), Math.round(). Let's explore them in this article.

Math.ceil function in JavaScript is used to round of a number that is passed into it to its nearest integer in upward direction of rounding. What to I mean by upward direction? Towards the greater value. Math.ceil() takes only one parameter that is the value to be rounded. So, if we have a value of 1.4, Math.ceil() will round off it to 2.
console.log(Math.ceil(1.4));
//2
console.log(Math.ceil(1.6));
// 2
console.log(Math.round(-1.4));
// 1
console.log(Math.round(-1.6));
// 1
Photo from Wikipedia
Where the Math.ceil method returns the smallest integer greater than or equal to the value we pass, Math.floor returns the largest or equal integer less than the given value. It also takes a single parameter.
Photo from Wikipedia
So, if we pass the same value 1.4 in Math.floor, we'll get 1 in return. Even if we pass 1.6, we'll also get 1.
console.log(Math.floor(1.4));
// 1
console.log(Math.floor(1.6));
// 1
console.log(Math.floor(-1.4));
// -2
console.log(Math.floor(-1.6));
// -2
Math.round() rounds off the number depending on the fractional part of the number. So, if the fractional part is >=.5, it'll return the smallest integer greater than the passed value and if the number is <=.4 we'll get the largest integer smaller than the number we pass.
console.log(Math.round(1.4));
// 1
console.log(Math.round(1.6));
// 2
console.log(Math.round(1.5));
// 2
console.log(Math.round(-1.4));
// -1
console.log(Math.round(-1.6));
// -2
console.log(Math.round(-1.5));
// 2
So, Math.round() can go both upward and downward depending on the fractional Part.

There's another method available in JS Math object that is Math.trunc(). Math.trunc() returns the integer part of a number by removing any fractional part of the number.
console.log(Math.trunc(1.4));
// 1
console.log(Math.trunc(1.6));
// 1
console.log(Math.trunc(-1.4));
// 1
console.log(Math.trunc(-1.6));
// 1
Rounding off numbers is an essential part of programming. I hope this article revisited your memories about different built-in rounding off methods we have in JavaScript. Leave an β€ if you found this article helpful. π