Javascript floor: The Complete Guide
JavaScript floor() is the function used to return the largest integer value that is less than or equal to a number. Floor values are often required in various algorithm-building scenarios dealing with mathematical operations and can be directly seen in different mathematical formulae.
Javascript floor
JavaScript floor() is a built-in math library function used to find the nearest integer less than the passed value(or equal to, if the given value is an integer). The floor() is a static method of Math, and it can be used without creating an object.
In other words, the floor() function rounds a number down and returns an integer value.
Syntax
Math.floor(x)
Parameters
The number whose floor value is to be calculated.
Return Value
For integral values, returns the passed value.
For non-integral values, returns the nearest integer less than the passed value.
See the following figure.
Note
- If no argument is passed, this method returns negative NaN.
- If any of the arguments cannot be converted into a valid number, this method returns NaN.
- If a parameter is null, this method returns 0.
- If a parameter is an empty string, this method returns 0.
- If a parameter is an empty array, this method returns 0.
- This method returns the same value for Math.ceil(x) and -Math.floor(-x).
Compatibility(Version and above)
- Google Chrome v1
- Internet Explorer v3
- Firefox v1
- Edge v12
- Opera
- Safari v1
- Android webview v1
- Chrome for Android v18
- Firefox for Android v4
- Opera for Android
- Safari on iOS v1
- Samsung Internet v1.0
- Node.js
JavaScript version: ECMAScript 1
Consider the following examples.
example1.js
The following example demonstrates the use of this method.
// example1.js var a = 34.23; var b = 32; console.log(Math.floor(a)); console.log(Math.floor(b));
Output
node example1 34 32
example2.js
The following example demonstrates the case no argument is passed.
// example2.js console.log(Math.floor());
Output
node example2 NaN
example3.js
The following example demonstrates the cases where NaN is returned and cases where it can be avoided.
// example3.js var a = "JavaScript"; // non-numeric string var b = [1, 2, 3, 4]; // array with more than one element var c; // undefined variable var d = {}; // empty object console.log(Math.floor(a)); console.log(Math.floor(b)); console.log(Math.floor(c)); console.log(Math.floor(d)); var e = "23.5"; //numeric string var f = [10.2]; //array with a single element console.log(Math.floor(e)); console.log(Math.floor(f));
Output
node example3 NaN NaN NaN NaN 23 10
example4.js
The following example demonstrates the cases where 0 is returned.
// example4.js var a = null; var b = ""; var c = []; console.log(Math.floor(a)); console.log(Math.floor(b)); console.log(Math.floor(c));
Output
node example4 0 0 0
example5.js
The following example demonstrates the case where a negative argument is passed.
// example5.js var a = -2.4; var b = 2.4; console.log(Math.floor(a)); console.log(Math.floor(b));
Output
node example5 -3 2
example6.js
The following example demonstrates that Math.ceil(x) returns same value as -Math.floor(-x).
// example6.js var x = 45.6; console.log(Math.ceil(x)); console.log(-Math.floor(-x));
Output
node example6 46 46
example7.js
The following example demonstrates a scenario where the floor method can be used. Consider the following problem.
A startup has decided to hire new engineers to build their product. But they have a fixed budget. Given the fixed budget and the salary of an engineer, find out the maximum number of engineers the startup can hire.
// example7.js var budget; var salary; const r = require('readline'); const rl = r.createInterface({ input: process.stdin, output: process.stdout }); const prompt1 = () => { return new Promise((resolve, reject) => { rl.question('Budget: ', (answer) => { budget = answer; resolve(); }); }); }; const prompt2 = () => { return new Promise((resolve, reject) => { rl.question('Salary: ', (answer) => { salary = answer; resolve(); }); }); }; const main = async () => { await prompt1(); await prompt2(); rl.close(); console.log(Math.floor(budget / salary)); } main();
Output
Test Case 1: ->node example7 Budget: 200000 Salary: 26000 7 Test Case 2: ->node example7 Budget: 250000 Salary: 100000 2