JavaScript Math.round[ ] function
Improve Article
Save Article
Like Article
- Last Updated : 29 Nov, 2022
Improve Article
Save Article
JavaScript Math.round[ ] function is used to round the number passed as a parameter to its nearest integer.
Syntax:
Math.round[value]
Parameters:
- value: The number to be rounded to its nearest integer.
Example 1: Rounding Off a number to its nearest integer
To round off a number to its nearest integer, the math.round[] function should be implemented in the following way:
Javascript
var
round =Math.round[5.8];
Number after rounding : 61
Number after rounding : 62
Number after rounding : 63
Number after rounding : 64
Output:
Number after rounding : 6
Example 2: Rounding Off a negative number to its nearest integer
The Math.round[] function itself rounds off a negative number when passed as a parameter to it. To round off a negative number to its nearest integer, the Math.round[] function should be implemented in the following way:
Javascript
var
Number after rounding : -60
Number after rounding : 61
Number after rounding : 62
Number after rounding : 63
Number after rounding : 64
Output:
Number after rounding : -6
Example 3: Math.round[] function, when a parameter has “.5” as a decimal
Below program shows the result of Math.round[] function when the parameter has “.5” as a decimal.
Javascript
var
Number after rounding : -12 Number after rounding : 131
Number after rounding : 61
Number after rounding : 62
Number after rounding : 63
var
Number after rounding : -12 Number after rounding : 138
Number after rounding : 61
Number after rounding : 62
Number after rounding : 63
Number after rounding : 64
Output:
Number after rounding : -12 Number after rounding : 13
We have a complete list of Javascript Math Objects methods, to check those please go through this Javascript Math Object Complete reference article.
Supported Browsers:
- Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer 3 and above
- Opera 3 and above
- Safari 1 and above
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
It is a common situation that you want less accuracy than PHP gives you, in which case you need to use one of PHP's selection of rounding functions: ceil[], floor[], and round[].
Both ceil[] and floor[] take just one parameter - the number to round. Ceil[] takes the number and rounds it to the nearest integer above its current value, whereas floor[] rounds it to the nearest integer below its current value. Here is an example:
After executing that code, $ceiled will be 5 and $floored will be 4.
Author's Note: The floor[] function converts a floating-point number to an integer in roughly the same way as typecasting, except typecasting is faster. The only difference is with negative numbers, where floor[] will round -4.5 down to -5 whereas typecasting would return -4.
The other function available is round[], which takes two parameters - the number to round, and the number of decimal places to round to. If a number is exactly half way between two integers, round[] will always round up.
As you can see, 4.5 is rounded up to 5, whereas 4.4999 is rounded down to 4. Line four has parameter two being used for the first time, which shows that it is very easy to round to a given number of decimal places. Note that in line five we get 4.1235 when rounding to four decimal places, because PHP looks one digit further to decide on the last digit. That is, if our number were 4.12344, rounding to four decimal places would give us 4.1234, but because the fifth digit is a 5, which is always rounded up, we find ourselves with 4.1235 instead.
The last example, $f, is a common situation encountered by people using round[]. Imagine you were organising a big trip to the countryside, and 1000 people signed up. You need to figure out how many coaches you need to hire, so you take the number of people, 1000, and divide it by the capacity of your coaches, 160, then round it to get a whole number, and you find the result is 6.
Where is the problem? Well, the actual result of 1000 / 160 is 6.25 - you need six and a quarter coaches to transport 1000 people, and you will only have ordered six because round[] rounded towards 6 rather than 7 because it was closer. As you cannot order six and a half coaches, what do you do? The solution is simple: in situations like this, you use ceil[]. All three rounding functions are useful in their own right, so be sure to try to remember them all.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!