Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the largest integer less than or equal to a number in JavaScript?
In this tutorial, we will learn how to get the largest integer that can be less than or equal to a number in JavaScript.
To get the largest integer less than or equal to a number, we use the Math.floor() method in JavaScript. It rounds down a number to its nearest integer towards the lower value, regardless of the decimal portion.
Following are two ways to find the largest integer that can be less than or equal to a number in JavaScript.
Using Math.floor() Method
The Math.floor() method returns the greatest integer that is less than or equal to a specified value. Because floor() is a static Math function, you should always use it as Math.floor() rather than as a method of a Math object you constructed.
It returns the integer that is greatest but less than or equal to the supplied number. Math is a built-in object that includes mathematical constants and functions, one of which is floor().
Syntax
Math.floor(value);
A decimal or integer value is supplied to the Math.floor() method as a parameter to compute the integer less than or equal to that value.
Example
The Math.floor() function helps to evaluate the nearest integer less than or equal to a number. In this example, we see how different values are processed by the floor() function.
<html>
<head>
<title>Get the largest integer less than or equal to a number using Math.floor() method</title>
</head>
<body>
<p id="result"></p>
<script>
let result = document.getElementById("result");
let value1 = Math.floor(90.9);
result.innerHTML += "First Value: " + value1 + "<br/>";
let value2 = Math.floor(-5.33);
result.innerHTML += "Second value: " + value2 + "<br/>";
let value3 = Math.floor(0.987);
result.innerHTML += "Third value: " + value3 + "<br/>";
let value4 = Math.floor(-0.188);
result.innerHTML += "Fourth Value: " + value4 + "<br/>";
</script>
</body>
</html>
First Value: 90 Second value: -6 Third value: 0 Fourth Value: -1
Notice how Math.floor(90.9) returns 90, Math.floor(-5.33) returns -6 (not -5), Math.floor(0.987) returns 0, and Math.floor(-0.188) returns -1. For negative numbers, it rounds towards negative infinity.
Using the Math.js Library
In this method, we use an external mathematical library called math.js. This library provides comprehensive mathematical functionality and is compatible with JavaScript's built-in Math library.
Math.js includes a versatile expression parser, a wide range of constants and built-in functions, and support for various data types such as integers, fractions, complex numbers, and matrices. It can run on any JavaScript engine and is easily extensible.
Syntax
math.floor(value);
Example
The math.floor() method works similarly to Math.floor(). This example shows how to use the math.js library by including it via a CDN script tag.
<html>
<head>
<script src="https://unpkg.com/mathjs/lib/browser/math.js"></script>
</head>
<body>
<p>Get the largest integer less than or equal to a number using math.js's <i>math.floor()</i> method</p>
<p id="result"></p>
<script>
let result = document.getElementById("result");
let value1 = math.floor(44.2);
result.innerHTML += "First Value: " + value1 + "<br/>";
let value2 = math.floor(-12.9);
result.innerHTML += "Second value: " + value2 + "<br/>";
let value3 = math.floor(0.99999987);
result.innerHTML += "Third value: " + value3 + "<br/>";
let value4 = math.floor(-72.01);
result.innerHTML += "Fourth Value: " + value4 + "<br/>";
let value5 = math.floor(1/2);
result.innerHTML += "Fifth Value: " + value5 + "<br/>";
</script>
</body>
</html>
Key Differences
| Method | Dependency | Performance | Use Case |
|---|---|---|---|
Math.floor() |
Built-in | Faster | Standard JavaScript applications |
math.floor() |
External library | Slightly slower | Complex mathematical operations |
Conclusion
Both Math.floor() and math.floor() provide the same functionality for getting the largest integer less than or equal to a number. Use the built-in Math.floor() for standard applications, and consider math.js when you need additional mathematical capabilities.
