
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get 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 is used to round off a number supplied as a parameter to its closest integer towards the downward direction of rounding, i.e., towards the less significant value.
Following are two ways in which we find the largest integer that can be less than or equal to a number in JavaScript.
Using Math.floor() Method
The method Math.floor() returns the greatest one 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() instead of using than as a method of a Math object you constructed (Math is not a constructor).
It returns the integer that is greatest but less than or equal to the supplied number. Math is a placeholder 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 that some variables are passed to the floor() function, and it returns a value.
<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 += "Fouth Value " + value4 + "<br/>"; </script> </body> </html>
The variable value1 takes an input of a decimal number and returns its nearest rounded-down integer. The variable value2 takes the input of a negative decimal integer. The value3 variable takes the input of a number nearby zero, which gives a zero value to the console.
A decimal number is passed to the Math.floor() function, and the result gets stored in the value4 variable.
Using the external mathematical library method
In this method, we will use an external modern mathematical library known as math.js. This library is used for complex computational facilities.
Math.js includes a versatile expression parser with symbolic computing support, a huge range of constants and built-in functions, and an integrated solution for working with various data types such as integers, fractions, complex numbers, fractions, and big numbers, fractions, units, and matrices.
It is a large math library for JavaScript. It is effective and simple to use. It is compatible with JavaScript's built-in Math library. It can run on any JavaScript engine. It is readily expandable. Math.js is an open-source technology.
Syntax
math.floor(value);
The value is supplied as a parameter to the math.floor() function.
Example
The math.floor() method may be used to get the nearest integer that is less than or equal to a given value. We can see in this example that several variables are supplied to the floor() method, which returns a result. The script tag is used to incorporate the math.js library.
<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 += "Fouth Value : " + value4 + "<br/>"; let value5 = math.floor(1/2); result.innerHTML += "Fifth Value : " + value5 + "<br/>"; </script> </body> </html>
In this tutorial, we have learned about the two techniques used to find the largest integer that can be less than or equal to a number in JavaScript. The first technique is to use the built-in Math library of JavaScript and its floor() function. The second technique is to use a mathematical library that can be applied to all the mathematical functions in the code.