JavaScript

How Do You Round to 1 Decimal Place in JavaScript?

Rounding to 1 decimal place in JavaScript is often necessary when working with numeric values. Rounding applies to a large number of decimal places, particularly when presenting data to users or performing calculations that require a certain level of precision.

This post will describe the methods to round down the number to one decimal place in JavaScript.

How to Round to 1 Decimal Place/Value Using JavaScript?

For rounding the number to the 1 decimal place, use the below-given methods:

Method 1: Round to 1 Decimal Place/value Using “Math.round()” Method

Use the “Math.round()” method which rounds a number to the nearest/closest integer value. For rounding the number to the 1 decimal place, first multiply the number by 10, round it to the nearest integer using Math.round(), and then divide the result by 10.

Syntax
For rounding a number to 1 decimal place, use the given syntax:

Math.round(number * 10) / 10;

Example
Create a variable that stores a decimal number “15.89345”:

var number = 15.89345;

Round the number using the Math.round() method:

var ans = Math.round(number * 10) / 10;

Finally, print the result on the console:

console.log(ans);

The number is successfully rounded to the 1 decimal place:

You can also pass the number directly to the Math.round() method as follows:

console.log(Math.round(982.4267 * 10) / 10);

Output

Method 2: Round to 1 Decimal Place/Value Using “toFixed()” Method

Another way for rounding the number to 1 decimal place is the “toFixed()” method. It rounds the number to the specified number of decimal points. For rounding a number to 1 decimal place utilizing toFixed(), specify “1” as the argument to the method.

Syntax
The following syntax is utilized for the toFixed() method:

num.toFixed(1)

Example
Call the toFixed() method on the number by passing “1”, which indicates that round the number to the 1 decimal place:

var ans = number.toFixed(1);

The output indicates that the number 15.89345 has been successfully rounded to the 1 decimal place:

You can also use this method as follows:

console.log(982.4267.toFixed(1));

Output

That’s all about rounding a number to the 1 decimal place in JavaScript.

Conclusion

To round the number to the 1 decimal place, use the “Math.round()” method or the “tofixed()” method. To round a number to 1 decimal place using Math.round() method, first, multiply the number by 10, round it to the nearest integer value using Math.round(), and then divide the result by 10. While using the toFixed(), specify “1” as the argument to the method. This post described the methods for rounding the number to one decimal place.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.