JavaScript

Round a Number (up or down) to the Nearest 100 in JavaScript

While solving mathematical problems, simple decimal point numbers does wonders in performing calculations. Also, in the case of keeping the value precise and simple simultaneously. In such cases, rounding a number up or down to the nearest 100 in JavaScript returns the numbers which are comparatively easier to work with, thereby omitting the unnecessary details.

This blog will demonstrate the approaches to round the number up or down to the nearest 100 using JavaScript.

How to Round a Number (up or down) to the Nearest 100 Using JavaScript?

A number can be rounded(up or down) to the nearest 100 in JavaScript using the following approaches:

  • round()” method.
  • floor()” and “Math.ceil()” methods.

Approach 1: Round a Number up/down to the Nearest 100 in JavaScript Using Math.round() Method

The “Math.round()” method rounds the specified number into the nearest integer. This method can be applied to round the particular number to the nearest up or down integer depending upon the passed number with the help of a user-defined function.

Syntax

Math.round(x)

In the given syntax:

x” represents the number that needs to be rounded.

Example

Let’s follow the below-given example:

<script type="text/javascript">
function roundNumber(number) {
return Math.round(number / 100) * 100;
}
console.log("The nearest up or down number is:", roundNumber(149));
console.log("The nearest up or down number is:", roundNumber(151));
</script>

Perform the following steps in the above lines of code:

  • Declare a function named “roundNumber()” has the number to be rounded as its parameter.
  • In its definition, firstly divide the passed number by “100” and round it. The rounded number will then be multiplied by 100 to get the rounded number again to the nearest 100.
  • Finally, access the defined function by passing the stated numbers as its parameter. This will result in rounding the specified numbers to the nearest 100.

Output

From the above output, it can be observed that the specified numbers are rounded to the nearest “100”.

Approach 2: Round a Number up/down to the Nearest 100 in JavaScript Using Math.ceil() and Math.floor() Methods

The “Math.ceil()” method rounds a number to the nearest up integer and the “Math.floor()” method rounds a number such that the nearest down integer is returned. These methods can be implemented such that the nearest up or down rounded number is achieved first and then multiplied by 100 to get the rounded number nearest to 100. This is applicable with the help of separate functions.

Syntax

Math.ceil(a)

In the given syntax:

a” corresponds to the number to be rounded to the nearest up integer.

Math.floor(x)

In the above syntax:

x” points to the number to be rounded to the nearest down integer.

Example

The following example illustrates the discussed concept:

<script type="text/javascript">
function roundUp(number){
return Math.ceil(number / 100) * 100;
}
function roundDown(number){
return Math.floor(number / 100) * 100;
}
console.log("The rounded up number is:", roundUp(149));
console.log("The rounded down number is:", roundDown(151));
</script>

In the above code snippet:

  • Declare a function named “roundUp()” that has the number to be rounded up to the nearest 100.
  • In its definition, apply the “ceil()” method such that the passed number is first divided by 100 and rounded to the nearest up integer. After that, it is multiplied by 100 to get the rounded number nearest to 100.
  • Likewise, define a function named “roundDown()”. Here, similarly, repeat the approach in the previous step, but this time, the rounded-up number to the nearest 100 will be computed using the “floor()” method.
  • Lastly, access both defined functions having the passed values to round them to an up or down number to the nearest 100, respectively.

Output

In the above output, it is evident that the numbers are rounded up or down respectively.

Conclusion

The “Math.round()” method or the “Math.floor()” and “Math.ceil()” methods can be utilized to round a number(up or down) to the nearest 100 in JavaScript. The former method can be implemented to simply round a number up as well as down to the nearest 100 depending upon the passed number. The latter methods can be applied to round a number up and down, respectively, with the help of separate functions. This tutorial explains how to round a number up or down to the nearest 100 using JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.