The Math.floor() function in JavaScript is used to round off the number passed as parameter to its nearest integer in Downward direction of rounding i.g towards the lesser value.
Syntax
Math.floor(value) Parameters : The number to be rounded to its nearest integer in downward rounding method. Returns : Result after rounding the number passed as parameter to the function. passed as parameter.
Example :
Input : Math.floor(4.23) Output : 4 Input : Math.floor(0.8) Output : 0
Errors and Exceptions
1. A non-numeric string passed as parameter returns NaN
2. An array with more than 1 integer passed as parameter returns NaN
3. An empty variable passed as parameter returns NaN
4. An empty string passed as parameter returns NaN
5. An empty array passed as parameter returns NaN
Below are some examples that illustrate the Math.floor() function in JavaScript:
<!-- NEGATIVE NUMBER EXAMPLE --><script type="text/javascript"> document.write(Math.floor(-2)); document.write(Math.floor(-2.56)); </script> |
Output:
-2 -3
<!-- POSITIVE NUMBER EXAMPLE --><script type="text/javascript"> document.write(Math.floor(2)); document.write(Math.floor(2.56)); </script> |
Output:
2 2
<!-- STRING EXAMPLE --><script type="text/javascript"> document.write(Math.floor("Geeksforgeeks")); </script> |
Output:
NaN
<!-- ADDITION INSIDE FUNCTION EXAMPLE --><script type="text/javascript"> document.write(Math.floor(7.2+9.3)); </script> |
Output:
16
Recommended Posts:
- JavaScript | toPrecision( ) Function
- Javascript | Number() Function
- JavaScript | Array some() function
- JavaScript | toString( ) function
- JavaScript | Math.E() function
- JavaScript | toFixed( ) Function
- JavaScript | Math.pow( ) Function
- JavaScript | Array.of() function
- JavaScript | Array every() function
- JavaScript | toExponential() Function
- JavaScript | Math.abs( ) function
- JavaScript | Math.cbrt() function
- JavaScript | Math.atanh() function
- JavaScript | Math.acosh() function
- JavaScript | Math.clz32() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



