# How to Round a Number in JavaScript

There are different ways to round a number in JavaScript:

**Math.round**– This follows the standard rounding rules. If the number is X.5 or higher, it will round up to the closest whole number. Else it will round down.**Math.floor**– This will always round down to the closest whole number.**Math.ceil**– This will always round up to the closes whole number.**toFixed({number of decimals})**– This will round the number to the number of decimals set as a parameter.

Let’s look at each method with examples

**Table of Contents**hide

## Math.round

With Math.round we are rounding to the nearest whole number. Let’s look at an example:

```
let number1 = 5.511235;
let round1 = Math.round(number1);
console.log(round1); // prints out 6
let number2 = 5.2;
let round2 = Math.round(number2);
console.log(round2); // prints out 5
```

The first number rounds up since it above .5. The second number rounds down since it is below .5.

## Math.floor

Math.floor rounds the number down to the nearest whole number no matter what the decimals are.

```
let number1 = 5.99;
let floor1 = Math.floor(number1);
console.log(floor1); // logs 5
let number2 = 5.1;
let floor2 = Math.floor(number2);
console.log(floor2); // logs 5
```

Here both numbers round down to 5.

## Math.ceil

Math.ceil does the opposite of Math.floor. With Math.ceil the number will round up no matter what the decimals are.

```
let number1 = 5.28;
let ceiling1 = Math.ceil(number1);
console.log(ceiling1); // logs 5
let number2 = 5.90;
let ceiling2 = Math.ceil(number2);
console.log(ceiling2); // logs 5
```

Both numbers will round down to 5 here.

## Shorten the Number of Decimals with ToFixed

Let’s say we have a decimal number with 8 decimals. How do we shorten that number to be 2 decimals?

We can do that with the toFixed method:

```
let number1 = 4.465424;
let fixed1 = number1.toFixed(2);
console.log(fixed1); // logs 4.47
let number2 = 7.89342;
let fixed2 = number2.toFixed(1);
console.log(fixed2); // logs 7.9
```

In the first example, we will shorten the number to 2 decimals by setting the number 2 as a parameter.

As you can see in the example, it will shorten the number by using the standard rounding formula.