Introduction to round() in JavaScript
The round() function in JavaScript is a math function. This function is used to round off the number to the nearest integer. The concept of rounding off the number is if the fractional part of the number is greater than or equal to 0.5, then the number will be rounded off to the next higher integer. Similarly, if the number is less than or equal to 0.5, the number will be rounded off to the previous integer present. In short, the round() function rounds off the number to the nearest integer.
Syntax
The syntax of the round function is as follows:
Round(x)
This is a user-defined function that accepts a single parameter. This is the number that will be rounded off. This function can be interpreted as:
Math.round(x)
Here Math is a predefined Javascript object. Round is used to get the value of the number, which is to be rounded off to the nearest integer. This function is a static method and hence is always used in math. Round (x). The return value or the result will be the number that is the nearest integer to the given number. Let us look at a few examples and see how the round function works.
Examples of round() in JavaScript
Different examples are mentioned below:
Example #1
console.log(Math.round(0.9));
In this example, the function math. Round () has the number 0.9. However, as per the working of the function, it should be rounded off to 1, as 1 is the nearest integer.
console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05));
Similarly, in the above example, three numbers are rounded off. The first one has the function of using the number 5.95. The closest integer to 5.95 is 6, which will be the output of this code. The next number is 5.5, which will be again rounded off to 6. Finally, the number 5.05 will be rounded to the previous integer, close to 5 more than 6.
console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95));
The above example is rounding off negative numbers. Here the first number will be rounded off to -5. Similarly, the second number will be rounded to -5. The largest number is -5. Finally, the third number will be rounded off to -6, as that integer is nearer.
Output:
The above screenshot shows the results of the preceding code that we had.
Example #2
Code:
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var num = 28.653
var result = Math.round(num * 100) / 100
document.write("<br />Result is: " +result);
var result1=Math.round(8.111111 * 1000)/1000
document.write("<br />Result is: " +result1);
</script>
</body>
</html>
Output:
Explanation:
var num = 28.653
var result = Math.round(num * 100) / 100 // returns 28.65
The above function uses a round function on the var num and does it to two decimal places. The result of this code will be 28.65
Similarly, we can round off the var num to only one decimal place. We can do it by using the following code:
var result=Math.round(num *10) / 10 // returns 28.5
Here the code will return the value of 28.5 as a result.
We can also round off the number by three decimal places. To do this, the following code will be helpful.
var result=Math.round(8.111111 * 1000)/1000 // returns 8.111
The result variable here uses the number 8.11111, which has a value of only up to three decimal places. We have used 1000 as we want the result up to 3 decimal places. The result here will hence be 8.111.
The main point here is the formula for rounding up too many decimal places of your choice. You can follow the below steps:
- Multiply any number as ten ^x that is 10, to the power of x.
- After doing this, you can easily apply the math.round () function.
- Once this is done, the result can be divided by ten ^ x.
This formula thus helps utilize the round function up to the number of digits you want. It can be the upper limit or lower limit as per the number. You can easily decide the number of digits by using this.
Example #3
Let us take an example explicitly to explain how the round() function works when there is a .5 in decimal.
Code:
<!DOCTYPE html>
<html>
<body>
<p id="example"></p>
<script type="text/javascript">
var round =Math.round(-15.5);
document.write("Number after rounding : " + round);
document.write("<br>")
var round =Math.round(9.51);
document.write("Number after rounding : " + round);
</script>
</body>
</html>
Output:
The above program will help you understand the working of the round function when there is .5.
The round function in JavaScript rounds off to the nearest integer. So, for example, the round function rounds off the number -15.5 to its nearest integer of 15. Similarly, when it finds a positive integer, the nearest value for this would be the next number. So the output for this would be 10. You can check the result for the above program below.
You can also face some errors or exceptions while running this function. These can be like the user will send a non-numeric string. This can result in NaN. Another cause can be when the user sends an empty variable as a parameter to this function. This will also result in NaN. Similarly, an empty string or an empty array can cause an issue of a similar kind. If an array with more than one integer is passed, there will also be an error or exception thrown.
Conclusion
JavaScript, like all languages, supports the Math library. The math library provides the round function responsible for rounding off the numbers. This round-off can be to the nearest integer preceding the number or succeeding the number. The function rounds these decimal values to the nearest integer. The round function can also be used with events. It has the capability of rounding off positive as well as negative numbers to their nearest integers. Hence, the round() function is important for businesses related to financial domains.