Updated March 2, 2023
The syntax of the round function is as follows:
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:
Different examples are mentioned below:
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.
The above screenshot shows the results of the preceding code that we had.
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.
Let us take an example explicitly to explain how the round() function works when there is a .5 in decimal.
The above program will help you understand the working of the round function when there is .5.
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.