For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
45. Packages in Java
52. Java Collection
55. Generics In Java
56. Java Interfaces
59. Streams in Java
62. Thread in Java
66. Deadlock in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
77. JUnit Testing
80. Jar file in Java
81. Java Clean Code
85. Java 8 features
86. String in Java
92. HashMap in Java
97. Enum in Java
100. Hashcode in Java
104. Linked List in Java
108. Array Length in Java
110. Split in java
111. Map In Java
114. HashSet in Java
117. DateFormat in Java
120. Java List Size
121. Java APIs
127. Identifiers in Java
129. Set in Java
131. Try Catch in Java
132. Bubble Sort in Java
134. Queue in Java
141. Jagged Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
150. parseInt in Java
152. Abstraction in Java
153. String Input in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
163. Deque in Java
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
176. Square Root in Java
189. Javafx
In Java, there are several functions to work on mathematical calculations. These are predefined functions that accept value and return results. One such handy mathematical function is the Math Floor Java which allows you to round a given decimal number to its nearest whole number or integer, similar to the Math.ceil Java method. However, unlike Math.ceil(), the floor function provides the largest integer that’s lesser than or equal to the given value.
The Math.floor() method is quite simple. Here, one needs to just give a desired decimal number as an input to the method, and the function will simply round it down to its nearest integer value. It can be applied to each element of an array.
Math Floor can be applied wherever one needs precise control over numerical operations, such as financial calculations and scientific research.
Math.floor() is a useful tool for solving various mathematical operations, especially when discarding a fractional part of a decimal value and working with whole numbers only. It is also noteworthy how this function works with negative numbers to ensure consistent rounding behaviors.
The floor() method in the Math class in Java is a built-in mathematical function for rounding up a decimal or floating-point number down to the nearest integer or whole number.
Here is the syntax of the Math.floor() method in Java:
public static double floor(double d)
Let us break down the syntax:
The Math.floor() method in Java returns a value of type double, the largest integer that is less than or equal to the specified value. The returned value is computed by rounding the specified value towards negative infinity.
For example, if we have a value d of 3.8, the floor() method will return 3.0 because 3 is the largest integer less than or equal to 3.8. Similarly, if we have a value d of -2.5, the method will return -3.0 because -3 is the largest integer less than or equal to -2.5.
double value = 3.8;
double result = Math.floor(value);
System.out.println(result); // Output: 3.0
The floor() method in Java does not throw any exceptions. It is a well-defined method in the Math class and is designed to handle the rounding down operation without any exceptional scenarios.
Therefore, when you use the floor() method, you don't need to handle any specific exceptions in your code. It will always return a result without throwing any exceptions.
However, it's important to note that if you pass NaN (Not-a-Number) or positive infinity as the argument to the floor() method, the result will be NaN or positive infinity, respectively. These are not exceptions but specific cases defined by the IEEE 754 floating-point standard.
public class upGradTutorials {
public static void main(String[] args) {
double num = 7.8;
double result = Math.floor(num);
System.out.println("Original number: " + num);
System.out.println("Floored number: " + result);
}
}
In the above example, we have a variable num initialized with the value 7.8. We use the Math.floor() method to round down the value of num to the nearest integer or whole number that is less than or equal to it. The result is stored in the variable result.
Finally, we print both the original and floored numbers to the console using System.out.println().
import java.util.Arrays;
public class upGradTutorials {
public static void main(String[] args) {
// Define an array of decimal numbers
double[] numbers = {2.5, 6.7, 4.1, 9.8, 3.3};
// Print the original array
System.out.println("Original array: " + Arrays.toString(numbers));
// Iterate through each element of the array and round it down using Math.floor()
for (int i = 0; i < numbers.length; i++) {
numbers[i] = Math.floor(numbers[i]);
}
// Print the modified array with floored values
System.out.println("Floored array: " + Arrays.toString(numbers));
}
}
In the above program, we have an array called numbers that contains decimal values. We want to round down each value in the array to the nearest integer or whole number.
First, we print the original array using the Arrays.toString() method. Then, we use a loop to iterate through each array element. Within the loop, we apply the Math.floor() method to round down each value to the nearest integer. The modified value is then returned to the same index in the array.
Finally, we print the modified array using the Arrays.toString() method to display the floored values.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class upGradTutorials {
public static void main(String[] args) {
// Create an ArrayList of decimal numbers
List<Double> numbers = new ArrayList<>(Arrays.asList(3.5, 9.1, 6.2, 4.7));
// Print the original ArrayList
System.out.println("Original ArrayList: " + numbers);
// Iterate through each element of the ArrayList and round it down using Math.floor()
for (int i = 0; i < numbers.size(); i++) {
double roundedDown = Math.floor(numbers.get(i));
numbers.set(i, roundedDown);
}
// Print the modified ArrayList with floored values
System.out.println("Floored ArrayList: " + numbers);
}
}
In this example, we create an ArrayList called numbers containing decimal values using the Arrays.asList() method. We want to round down each value in the ArrayList to the nearest integer.
First, we print the original ArrayList using the System.out.println() statement.
Then, we iterate through each element of the ArrayList using a loop. Within the loop, we use the Math.floor() method to round down each value to the nearest integer. The rounded-down value is then returned to the ArrayList using the set() method.
Finally, we print the modified ArrayList containing the floored values using the System.out.println() statement.
public class upGradTutorials {
public static void main(String[] args) {
double num = 7.8;
double floored = Math.floor(num);
System.out.println("Original number: " + num);
System.out.println("Floored number: " + floored);
}
}
In this example, we have a variable num initialized with the value 7.8. The Math.floor() method is used to round down the value of num to the nearest integer or whole number that is less than or equal to it. The rounded-down value is stored in the variable floored.
Finally, we print both the original and floored numbers to the console.
public class upGradTutorials {
public static void main(String[] args) {
double num = -5.3;
double floored = Math.floor(num);
System.out.println("Original number: " + num);
System.out.println("Floored number: " + floored);
}
}
First, we declare a variable num and assign it the value -5.3, representing a negative decimal number. Next, we call the Math.floor() method and pass num as the argument. This method returns the largest integer value that is less than or equal to the given number.
The result of the Math.floor() method is stored in the variable floored. Finally, we use System.out.println() statements to display the original number (num) and the floored number (floored) on the console.
From what we learned from this tutorial, it is clear how the Math.floor() function can perform precise calculations in a code. Thus, making it a must-learn function for all the programmers out there.
There are many other uses for the Math.floor(), such as its application in converting the Java floor double to int just by casting the result. This simplicity in its application, coupled with the accurate results it delivers, makes this function an essential part of a programmer's toolkit.
Thus, with the understanding of Math.floor() one can draw out the most precise and accurate calculations while solving the difficult programming challenges.
Math floor in Java is a mathematical function in the programming language that rounds down a given decimal number to the nearest integer. To be precise, it returns the largest integer less than or equal to the given number while discarding the decimal part.
As we know, the Math.floor() function rounds the given number to its nearest integer, which will be less than or equal to the number. Therefore, in this case, the result of the Math.floor(-4.7) is -5 .
To apply the floor function in Java, one can simply use the Math.floor() method. One must pass their desired decimal number as an argument to the function to apply it. In return, it will give out the result by rounding it down to the largest integer that is less than or equal to the given number.
Math.floor is a JavaScript function that rounds a number down to the nearest integer, returning the largest integer less than or equal to the given number.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.