📜  javascript floor - Javascript (1)

📅  最后修改于: 2023-12-03 15:16:05.184000             🧑  作者: Mango

JavaScript floor

In JavaScript, the Math.floor() method is used to round down a decimal number to its nearest integer. It returns the largest integer less than or equal to the given number.

Syntax
Math.floor(x)
  • x: Required. The number to round down.
Example
let num1 = 5.5;
let num2 = 9.9;
let num3 = -4.6;

console.log(Math.floor(num1)); // Output: 5
console.log(Math.floor(num2)); // Output: 9
console.log(Math.floor(num3)); // Output: -5

In the above example, we have declared three variables with decimal numbers. We have used Math.floor() method to round down these numbers to their nearest integer.

Conclusion

Math.floor() is a useful method in JavaScript for rounding down decimal numbers to their nearest integers. It works by returning the largest integer less than or equal to the given number.