The floor()
method rounds the specified double value downward and returns it. The rounded value will be equal to a mathematical integer. That is, the value 3.8 will be rounded to 3.0 which is equal to integer 3.
Example
class Main {
public static void main(String[] args) {
double a = 3.8;
System.out.println(Math.floor(a));
}
}
// Output: 3.0
Math.floor() Syntax
The syntax of the floor()
method is:
Math.floor(double value)
Here, floor()
is a static method. Hence, we are accessing the method using the class name, Math
.
Math.floor() Parameters
The floor()
method takes a single parameter.
- value - number which is to be rounded upward
Math.floor() Return Value
- returns the rounded value that is equal to the mathematical integer
Note: The returned value is the largest value that is smaller than or equal to the specified argument.
Example: Java Math.floor()
class Main {
public static void main(String[] args) {
// Math.floor() method
// value greater than 5 after decimal
double a = 1.878;
System.out.println(Math.floor(a)); // 1.0
// value equals to 5 after decimal
double b = 1.5;
System.out.println(Math.floor(b)); // 1.0
// value less than 5 after decimal
double c = 1.34;
System.out.println(Math.floor(c)); // 1.0
}
}