# Round a Number to 1 Decimal Place in JavaScript

Last updated: Jul 27, 2022

Photo from Unsplash

## Round a Number to 1 Decimal Place#

To round a number to 1 Decimal place:

1. Use the `Math.round()` function to round the result of multiplying the number by `10`.
2. The `Math.round` function will round the result to the nearest integer.
3. Divide the integer by `10` to get the number rounded to 1 decimal place.
index.js
```Copied!```const num = 5.566;
const result = Math.round(num * 10) / 10;

console.log(result); // 👉️ 5.6
``````

We used the Math.round function to round the result of multiplying the number by `10` to the nearest integer.

Here are some examples of how the `Math.round` function works.

index.js
```Copied!```console.log(Math.round(2.22)); // 👉️ 2
console.log(Math.round(2.5)); // 👉️ 3

console.log(Math.round(2.22 * 10)); // 👉️ 22
console.log(Math.round(2.5 * 10)); // 👉️ 25
``````

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to `0.5`, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than `0.5`, it gets rounded to the lower absolute value.

The last step is to divide the integer by `10` to get the result of rounding the number to 1 decimal place.

index.js
```Copied!```// 👇️ 2.5
console.log(Math.round(2.456789 * 10) / 10);

// 👇️ 4.7
console.log(Math.round(4.689 * 10) / 10);
``````

Alternatively, you can use the `Number.toFixed` method.

## Round a Number to 1 Decimal Place using toFixed()#

Use the `toFixed()` method to round a number to 1 decimal place, e.g. `num.toFixed(1)`. The `toFixed` method formats a number to a specified number of decimal places and rounds the number if necessary.

index.js
```Copied!```const num1 = 5.566;
const result1 = num1.toFixed(1);
console.log(result1); // 👉️ 5.6
console.log(typeof result1); // 👉️ string

// 👇️ if the value is a string
// call parseFloat to convert it to a number first
const str1 = '5.666';
const result2 = parseFloat(str1).toFixed(1);
console.log(result2); // 👉️ 5.7
console.log(typeof result2); // 👉️ string

// 👇️ Convert string back to a number
const num2 = 5.05;
const result3 = Number(num2.toFixed(1));
console.log(result3); // 👉️ 5
console.log(typeof result3); // 👉️ number
``````

In the first example, we used the Number.toFixed method to round a number to 1 decimal place.

The only parameter the method takes is the number of digits that should appear after the decimal point.

The `toFixed` method returns a string representation of the number.

In our second example, we have a string that is a valid number. We used the parseFloat function to convert the string to a number, because the `toFixed` method can only be called on numbers.

index.js
```Copied!```const str1 = '5.666';
const result2 = parseFloat(str1).toFixed(1);
console.log(result2); // 👉️ 5.7
console.log(typeof result2); // 👉️ string
``````

In the third example, we used the `Number` object to convert the string that the `toFixed` method returned to a number.

index.js
```Copied!```const num2 = 5.05;
const result3 = Number(num2.toFixed(1));
console.log(result3); // 👉️ 5
console.log(typeof result3); // 👉️ number
``````
However, notice that the trailing zero was dropped. This is because JavaScript doesn't keep any insignificant, trailing zeros around.

The number `5.00` is the same as `5`, so the trailing zeros get dropped when the value is converted to a number.

index.js
```Copied!```console.log(5.00 === 5); // 👉️ true
``````
Floating point numbers don't represent all decimals precisely in binary, which can lead to inconsistent results.
index.js
```Copied!```console.log(0.1 + 0.2 === 0.3); // 👉️ false
``````

The sum of `0.1` and `0.2` is actually equal to `0.30000000000000004` instead of `0.3`. This is because the binary floating-point format cannot accurately represent numbers like `0.1` or `0.2`.

The code gets rounded to the nearest number, resulting in a rounding error.