Java Math floor() Method
The floor() method of Math class returns the largest double value that is equal to a mathematical integer and is less than or equal to the argument.
Syntax:
public static double floor(double a)
Parameters:
The parameter ‘a’ represents the value.
Return Value:
The floor () method returns the largest floating-point value that is equal to a mathematical integer and is less than or equal to the argument.
Special cases of the floor() method are as follows:
- It returns the result same as the argument, if the argument passed is equal to an integer or is a NaN or an infinity or positive zero or negative zero value
Example 1:
public class JavaMathFloorExample1 { public static void main(String[] args) { double a=10.780; //returns the largest double value that is less than or equal to the a System.out.println("Largest double value of "+ a+" = "+Math.floor(a)); } }
Output:
Largest double value 0.0 = 0.0
Example 2:
public class JavaMathFloorExample2 { public static void main(String[] args) { double a=10.780/0.0d; //It returns the result same as 'a', if 'a' is equal to positive infinity System.out.println("Largest double value of "+ a+" = "+Math.floor(a)); } }
Output:
Largest double value of Infinity = Infinity
Example 3:
public class JavaMathFloorExample3 { public static void main(String[] args) { double x=-110.780/0.0d; //It returns the result same as x, if x is equal to a Negative infinity System.out.println("Largest double value of "+ x+" = "+Math.floor(x)); } }
Output:
Largest double value of -Infinity = -Infinity
Example 4:
public class JavaMathFloorExample4 { public static void main(String[] args) { double a=0.0/0.0d; //It returns the result same as a, if a is equal to NaN System.out.println("Largest double value of "+ a+" = "+Math.floor(a)); } }
Output:
Largest double value of NaN = NaN
Example 5:
public class JavaMathFloorExample5 { public static void main(String[] args) { double a=0.0d; //It returns the result same as a, if a is equal to zero System.out.println("Largest double value of "+ a+" = "+Math.floor(a)); } }
Output:
Largest double value of 0.0 = 0.0