# How to Round a Number in JavaScript

## Table of Contents

Rounding a number can be a useful thing to do when you want to display a number in a more readable format.

For example, sometimes showing an exact number is not necessary, and you can round it to the nearest whole number.

In this article, we will look at how to round a number in JavaScript.

## Using Math.round()

The most straightforward way to round a number in JavaScript is to use the `Math.round()`

function.

This function takes in a number and returns the nearest whole number.

First, let's define a variable called `number`

and assign it the value of `3.14`

:

```
const number = 3.14;
```

Now, let's round this number using the `Math.round()`

function:

```
const number = 3.14;
const roundedNumber = Math.round(number);
console.log(roundedNumber);
```

```
3
```

As you can see, the `roundedNumber`

variable now has the value of `3`

because `3.14`

was rounded to the nearest whole number.

## Using Math.ceil()

Another way to round a number in JavaScript is to use the `Math.ceil()`

function.

This function takes in a number and returns the smallest whole number that is greater than or equal to the given number.

In other words, it rounds a number up to the nearest whole number.

Let's see how this works in practice.

```
const number = 3.14;
const roundedNumber = Math.ceil(number);
console.log(roundedNumber);
```

```
4
```

As mentioned before `Math.ceil()`

rounds a number up to the nearest whole number so `3.14`

was rounded up to `4`

.

## Using Math.floor()

The `Math.floor()`

function is similar to `Math.ceil()`

but it rounds a number down to the nearest whole number.

Let's see an example of this:

```
const number = 3.14;
const roundedNumber = Math.floor(number);
console.log(roundedNumber);
```

```
3
```

No surprises here, `3.14`

was rounded down to `3`

.

## Conclusion

In this article, we looked at how to round a number in JavaScript.

Your options include using the `Math.round()`

function, `Math.ceil()`

function, or `Math.floor()`

function.

Thanks for reading!

- How to Install Node on Windows, macOS and Linux
- How to Serve Static Files with Nginx and Docker
- Best Visual Studio Code Extensions for 2022
- How to build a Discord bot using TypeScript
- Getting Started with Handlebars.js
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Getting User Location using JavaScript's Geolocation API
- Building a Real-Time Note-Taking App with Vue and Firebase
- Getting Started with Vuex: Managing State in Vue
- Setting Up a Local Web Server using Node.js
- Using Axios to Pull Data from a REST API
- How To Create a Modal Popup Box with CSS and JavaScript