How to get the value of a number rounded to the nearest integer in JavaScript?


In this tutorial, we will learn how to get the value of a number rounded to the nearest integer in JavaScript.

The nearest integer of a number can be found in different ways in JavaScript. In this tutorial, we will see the two most popular ways of doing it−

  • Using Math.round() method

  • Without using the Math.round() method

Rounding a number means taking the nearest integer value from that number. It is useful for mathematical calculations when you need an approximate value to simplify your calculation.

For example: If you have a number like 0.9999999, then taking this number into mathematical calculations like multiplication or division can be a challenging thing to do. For that reason, we round the number to the nearest integer to simplify the calculation. In this case, the nearest number from 0.9999999 is 1, so we take 1 in the calculation instead of 0.9999999.

Using the Math.round() Method

In JavaScript, the Math.round() method is useful to round a number to its nearest integer number. It takes a variable as a parameter expected to be a number and returns the nearest integer of that number. Otherwise, if the parameter is empty or the variable is a non−numeric value, it returns NaN, which denotes “Not a Number.” Look at the following examples.

// For Positive numbers Math.round(2.2) // 2 Math.round(2.7) // 3 Math.round(2.5) // 3 // For Negative numbers Math.round(-2.2) // -2 Math.round(-2.7) // -3 Math.round(-2.5) // -2 // For empty argument or Non-numeric value Math.round() // NaN Math.round('abcd') // NaN

Syntax

Users can follow the below syntax to get the value of a number rounded to the nearest integer using the Math.round() method.

Math.round(number)

Parameters

  • number − it takes a variable that is expected to be a number.

Return Type

  • rounded the nearest integer of the given number

  • NaN − if the argument is empty or a non−numeric value.

Example

In the below example, we have used the Math.round() method to get the value of a number rounded to the nearest integer. We have taken the different values to observe the output of the Math.round() method

<html> <body> <h3>Get the value of a number rounded to the nearest integer using <i>Math.round()</i> method in JavaScript</h3> <div id="root"></div> <script> let number1 = 2.1 let number2 = 2.6 let number3 = 2.5 let number4 = -2.5 let number5 = -2.51 let text = 'abcd' let root = document.getElementById('root') root.innerHTML ='The number ' +number1 +' rounded to the nearest integer: ' +Math.round(number1) + '<br>' root.innerHTML +='The number ' +number2 +' rounded to the nearest integer: ' +Math.round(number2) + '<br>' root.innerHTML +='The number ' +number3 +' rounded to the nearest integer: ' +Math.round(number3) + '<br>' root.innerHTML +='The number ' +number4 +' rounded to the nearest integer: ' +Math.round(number4) + '<br>' root.innerHTML +='The number ' +number5 +' rounded to the nearest integer: ' +Math.round(number5) + '<br>' root.innerHTML +='The text ' +text +' rounded to the nearest integer: ' +Math.round(text) + '<br>' </script> </body> </html>

In the above output, users can see that Math.round() method returns the rounded nearest integer of the number, and for the non-numeric value, it returns NaN.

Without using the Math.round() Method

The rounded number to the nearest integer can also be found without the Math.round() method using a simple calculation and ParseInt() method.

ParseInt() method takes a string or any number and converts it into an Integer; if the string is non-numeric, it returns NaN.

Syntax

function round(number) {

   // positive number
   if (number > 0) {
      number = number + 0.5
   }
   
   // negative number
   else if (number < 0) {
      number = number - 0.5
   }
   return parseInt(number)
}

Algorithm

Step 1 − Declare a function and take a number as an argument.

Step − if the number is positive, then add 0.5 with it or if the number is negative, then subtract 0.5 with it.

Step − use the parseInt() and pass the number in the method and return the value

Note This algorithm returns -3 for the number -2.5 whereas the Math.round(-2.5) returns -2. Same for all numbers with .5 in their decimal

Example

In the below example, we find out the value of a number rounded to the nearest integer without the Math.round(). We have taken the different values to observe the output.

<html> <body> <h4>Get the value of a number rounded to the nearest integer without using<i> Math.round() </i> method in JavaScript</h4> <div id = "root"> </div> <script> let number1 = 2.1 let number2 = -2.6 let number3 = 2.5 let text = 'abcd' function round(number) { if (number > 0) { number = number + 0.5 } else if (number < 0) { number = number - 0.5 } return parseInt(number) } let root = document.getElementById('root') root.innerHTML ='The number ' +number1 +' rounded to the nearest integer: ' + round(number1) + '<br>' root.innerHTML +='The number ' +number2 +' rounded to the nearest integer: ' +round(number2) + '<br>' root.innerHTML +='The number ' +number3 +' rounded to the nearest integer: ' +round(number3) + '<br>' root.innerHTML +='The text ' +text +' rounded to the nearest integer: ' +round(text) + '<br>' </script> </body> </html>

In the above output, users can see that the round method returns the rounded nearest integer value.

We have learned how to get the value of a number rounded to the nearest integer in JavaScript with and without using Math.round() method. The first approach is the standard approach, and the second approach can only be used if the users don’t want to use the Math.round(). It is recommended to use the Math.round() as it is a built−in method of JavaScript for doing this.

Updated on: 14-Sep-2022

895 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements