JavaScript

How to Round a Number to the Nearest 10 in JavaScript

A number’s approximate calculation is known as rounding. It helps make numbers clearer and simpler to understand. According to the required accuracy of the calculation, numbers can be rounded to a specific value. A number is rounded to the nearest tenth, the whole number is not changed, just the approximate value is changed.

This study will explain the procedure to round a number in JavaScript to the nearest 10.

How to Round a Number to the Nearest 10 in JavaScript?

In JavaScript use the following methods, to round a number to the nearest 10:

Let’s see the working of the above-mentioned methods one by one!

Method 1: Round a Number to the Nearest 10 Utilizing Math.round() Method

In JavaScript the “round()” method of the “Math” type is used to round the decimal numbers as well as the whole numbers on the approximated value. It rounds the integer to the next whole number.

Syntax

Follow the given syntax for using the round() method to round the number nearest to the 10:

Math.round(number / 10) * 10
  • The “Math.round()” method is invoked by passing the number divided by 10 as an argument that will round the result to the nearest whole number.
  • Then, multiply it by 10 which will round the result to the nearest 10.

Example

First, define a function named “roundToNearest10” with a parameter “number”. Calls the Math.round() method that will return the approximated value that is nearest to the 10:

function roundToNearest10(number) {

return Math.round(number / 10) * 10;

}

Call the “roundToNearest10” function by passing a whole number “6745”. It will first be divided by 10 and returns the “674.5” which will be rounded to “675” which is the nearest whole number of the 674.5. The resultant value will then be multiplied by 10 to get the approximated value to the nearest 10:

console.log(roundToNearest10(6745));

The output will show “6750” which is the nearest 10 to the “6745”:

Let’s pass the decimal value “89.9” in the function and see the rounded value:

console.log(roundToNearest10(89.9));

The output will print “90” by rounding the decimal number “89.9” to the nearest 10:

Method 2: Round a Number to the Nearest 10 Utilizing Math.ceil() Method

To round a number to the nearest 10, the “Math.ceil()” method is used. It will round the number to the upcoming largest integer. If a decimal number is passed to the Math.ceil() method, it returns the whole number.

Syntax

The given syntax is used for the “ceil()” method:

Math.ceil(number / 10) * 10
  • It takes a number as an argument divided by 10 and then multiplies it with 10.
  • By dividing the number by 10 it will round the number to the next upcoming largest integer.
  • Then, multiply the resultant number by 10 for getting the number rounded up to the nearest 10.

Example

Invoke the “Math.ceil()” method in “roundToNearest10” function by passing number divided by 10 and then multiply it with 10 to round the number to the nearest 10:

function roundToNearest10(number) {

return Math.ceil(number / 10) * 10;

}

Call the “roundToNearest10” function and pass a number “6745” as an argument. It will first be divided by 10 and returns the “674.5” which will be rounded to “675” due to the ceil () method that is the next largest integer of 674.5. Then, the resultant number will be multiplied by 10 and get the approximated value to the nearest 10:

console.log(roundToNearest10(6745));

Output

Similarly, the decimal number is also rounded to the nearest 10 in JavaScript using the Math.ceil() method. Pass the number “78.02” as a parameter in the “roundToNearest10” function. It will return “8” which is the next largest integer of the “7.802”, and then multiply the resultant number by 10 that is the approximated value to the nearest 10:

console.log(roundToNearest10(78.02));

The corresponding output will be:

Method 3: Round a Number to the Nearest 10 Utilizing Math.floor() Method

There is another method “Math.floor()” that is used to round a number to the nearest 10. It will round the number down to the nearest integer. If a decimal integer is passed to the Math.floor() method, it returns the nearest whole integer.

Syntax

The following syntax is used for the floor() method:

Math.floor(number / 10) * 10
  • The method is called by passing the number divided by 10 as an argument that will round the resultant number down to the nearest integer.
  • Then, the resultant number will multiply by 10 which will return the number rounded to the nearest 10.

Example

In the defined function “roundToNearest10()”, call the “Math.floor()” method by passing number divided by 10 as an argument and then, multiply it with 10:

function roundToNearest10(number) {

return Math.floor(number / 10) * 10;

}

Pass the number “6745” as an argument in the defined function named “roundToNearest10()”. It will first be divided by 10 and returns the “674.5” which will be rounded to “674” due to the floor() method that is the nearest down integer of 674.5. Then, the resultant number “674” will be multiplied by 10 and get the approximated value to the nearest 10:

console.log(roundToNearest10(6745));

Output

Pass the decimal number “-5.15” as a parameter in the “roundToNearest10” function. It will return “7” which is the nearest down integer of the “7.802”, and then multiply the resultant number by 10 which is the approximated value to the nearest 10:

console.log(roundToNearest10(-5.15));

The output will be:

Conclusion

To round a number to the nearest 10, use the JavaScript’s predefined methods which include Math.round(), Math.ceil() and the Math.floor(). The Math.round() method rounds the number to the nearest whole integer The Math.ceil() method rounds the number to the next largest integer, while the Math.floor() method rounds the number to the nearest down integer. All these methods will multiply by 10 to round the resultant number to the nearest 10. In this study, the working of all these methods has been explained along with their examples.

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.