Java Math floor()

floor() accepts double value as an argument and returns the largest integer which is less than or equal to the argument. The returned value is of type double.

Following is the syntax of floor() method.

</>
Copy
double result = floor(double value)

Since the definition of floor() function has double datatype as argument, you can pass int, float or long as arguments; because these datatypes could implicitly promote to double.

Example 1 – Math.floor(double)

In the following example, we pass a double value as argument to floor() method and find its floor value.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		double x = 3.14D;
		double a = Math.floor(x);
		System.out.println(a);
	}
}

Output

3.0

Example 2 – Math.floor(float)

In the following example, we pass a float value as argument to floor() method and find the ceiling integer of its value.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		float x = 2.365F;
		double a = Math.floor(x);
		System.out.println(a);
	}
}

Output

2.0

Example 3 – Math.floor(int)

In the following example, we pass a int value as argument to floor() method and find its ceiling value.

If the argument is already a mathematical integer value, the return value is same as that of the argument.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		int x = 3;
		double a = Math.floor(x);
		System.out.println(a);
	}
}

Output

3.0

Example 4 – Math.floor(long)

In the following example, we pass a long value as argument to floor() method and find its ceiling value.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		long x = 3999999999999L;
		double a = Math.floor(x);
		System.out.println(a);
	}
}

Output

3.999999999999E12

Example 5 – Math.floor() – With Negative Number Argument

In the following example, we pass a negative value as argument to floor() method and find its ceiling value.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		double x = -12.4;
		double a = Math.floor(x);
		System.out.println(a);
	}
}

Output

-13.0

Example 6 – Math.floor() – Arguments of NaN, Negative Infinity, Positive Infinity

For the arguments of NaN, negative infinity or positive infinity; floor() returns the same value as that of argument.

Java Program

</>
Copy
public class MathExample {
	public static void main(String[] args) {
		System.out.println(Math.floor(Double.NaN));
		System.out.println(Math.floor(Double.POSITIVE_INFINITY));
		System.out.println(Math.floor(Double.NEGATIVE_INFINITY));
	}
}

Output

NaN
Infinity
-Infinity

Conclusion

In this Java Tutorial, we learned about Java Math.floor() function, with example programs.