Categories
JavaScript Answers

How to round to whole numbers in JavaScript?

Spread the love

Sometimes, we want to round to whole numbers in JavaScript.

In this article, we’ll look at how to round to whole numbers in JavaScript.

How to round to whole numbers in JavaScript?

To round to whole numbers in JavaScript, we can use some Math methods.

For instance, we write

const ceiling = Math.ceil(num);
const floor = Math.floor(num);
const rounded = Math.round(num);

to call Math.ceil to get the ceiling of number num.

We call Math.floor to get the floorof number num.

And we call Math.round to get num rounded with banker’s rounding.

Conclusion

To round to whole numbers in JavaScript, we can use some Math methods.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *