AppDividend
Latest Code Tutorials

Javascript Math floor() Function | floor() in Javascript

0

JavaScript Math floor() is an inbuilt function that is used to find the nearest integer less than the passed value(or equal to, if the passed value is itself an integer). Floor values are often required in various algorithm-building scenarios dealing with mathematical operations and can be directly seen in different mathematical formulae as well.

Javascript Math floor()

JavaScript floor() is the function that is used to return the largest integer value that is less than or equal to a number. In other words, the floor() function rounds a number down and returns an integer value. Since floor() is a static method of Math, it can be used without creating an object.

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.

 

Javascript Math floor()

Note

  1. If no argument is passed, this method returns negative NaN.
  2. If any of the arguments cannot be converted into a valid number, this method returns NaN.
  3. If a parameter is null, this method returns 0.
  4. If a parameter is an empty string, this method returns 0.
  5. If a parameter is an empty array, this method returns 0.
  6. This method returns the same value for Math.ceil(x) and -Math.floor(-x).

Compatibility(Version and above)

  1. Google Chrome v1
  2. Internet Explorer v3
  3. Firefox v1
  4. Edge v12
  5. Opera  
  6. Safari v1
  7. Android webview v1 
  8. Chrome for Android v18
  9. Firefox for Android v4
  10. Opera for Android 
  11. Safari on iOS v1
  12. Samsung Internet v1.0
  13. 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

See also

Javascript Math.random()

Javascript Math.cbrt()

Javascript Math.exp()

Javascript Math.log()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.