➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Math.floor JavaScript | As it contains either a package or a library for practically every difficulty a programmer might have, JavaScript is one of the most well-known programming languages right now. You must deal with numbers whether constructing a JavaScript program or a full-stack online application. You may not like a floating value in your variable in some instances, which is where the Math.floor() comes in.
Let’s look at the Math.floor JavaScript function performs before we glance at where it might be applied.
What Does Math.floor Mean In JavaScript? The math.floor() function returns the smallest or equal integer that is smaller or equal to the specified number. The Math.floor method is used to round a number supplied as a parameter to the closest integer in the Negative direction of rounding, i.e. towards the lower number.
The placeholder object Math must be used since the floor method is a static method under Math. It goes without saying that arguments can only be integers or floating numbers.
In this tutorial, we’ll look at how the math module’s floor function is utilized and its restrictions and associated subjects.
Syntax:–
Math.floor(value)
As noted before and as described below, this method entails only one parameter:-
- The value to be verified for the Math.floor is called value.
- The least integer higher than or equal to the provided value is returned by the Math.floor() function.
The Math floor() method in JavaScript is demonstrated in the following examples:-
Value | Math.floor(Value) |
0.32 | 0 |
1.534 | 1 |
8657.766 | 8657 |
-0.32 | -1 |
-1.001 | -2 |
Math.floor JavaScript Examples
If a negative value is supplied as a parameter:–
console.log(Math.floor(-0.32));
console.log(Math.floor(-1.001));
console.log(Math.floor(-9.4));
Output:-
-1
-2
-10
Passing zero as a parameter:–
console.log(Math.floor(0));
Output:-
0
Passing positive value as a parameter:–
console.log(Math.floor(0.32));
console.log(Math.floor(1.534));
console.log(Math.floor(8657.766));
Output:-
0
1
8657
Math.floor And Math.ceil In JavaScript
The math.floor() function returns the smallest or equal integer that is smaller or equal to the specified number. The Math.floor method is used to round a number supplied as a parameter to the closest integer in the Negative direction of rounding, i.e. towards the lower number.
Furthermore, the JavaScript Math.ceil() function will return the most significant or equal integer to the specified value.
Value | Math.floor(Value) | Math.ceil(Value) |
0.32 | 0 | 1 |
1.534 | 1 | 2 |
8657.766 | 8657 | 8658 |
-0.32 | -1 | -0 |
-1.001 | -2 | -1 |
Limitations
It’s better to avoid using Math.floor when working with money because it always returns the lowest number. Here, Round() will be a better option. If null is given as an argument, Math.floor returns 0.
This brings us to the end of the article, where we learned how the Math.floor() function works.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!