Java math floor function is used to get largest integer which is lesser than number.
Syntax
1 2 3 |
public static double floor(double x) |
Example
Let’s use math’s floor function in the example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; public class MathFloorMain { public static void main(String[] args) { // Use math.floor to get largest integer lesser // than number System.out.println("floor value for 2.6 = " + Math.floor(2.6)); System.out.println("floor value for 26.1 = " + Math.floor(26.1)); System.out.println("floor value for -0.8 = " + Math.floor(-0.8)); System.out.println("floor value for -6.2 = " + Math.floor(-6.2)); float f=4.6f; System.out.println("floor value for 4.6f = " + Math.floor(f)); } } |
When you run above program, output of the program will be:
Ceil value for 2.6 = 3.0
Ceil value for 26.1 = 27.0
Ceil value for -0.8 = -0.0
Ceil value for -6.2 = -6.0
Ceil value for 4.6f = 5.0
Ceil value for 26.1 = 27.0
Ceil value for -0.8 = -0.0
Ceil value for -6.2 = -6.0
Ceil value for 4.6f = 5.0
Few points about Math.ceil function:
- In case of Nan or infinity or zero, it will give you same result as argument.
- If you pass a value which is already equal to integer, it will result in same value.
That’s all about Java math floor function.