Floor

Math Class

Floor

🙋 Need help? Ask an expert now!

The floor function returns the largest whole number that is less than or equal to the input. This method only takes and returns double values. Take a look at the example below.

FloorMethodExample.java
package exlcode;
            
            public class FloorMethodExample {
            
              // Returns largest integer that is less than or equal to the argument
              public static double exampleVariableOne = Math.floor(10.4);
              public static double exampleVariableTwo = Math.floor(-20.4);
            
              public static void main(String[] args) {
                System.out.println(exampleVariableOne);
                System.out.println(exampleVariableTwo);
              }
            }

As the nearest whole number that is less than or equal to 10.4 is 10, 10.0 is printed. Similarly, since the closest whole number that is less than or equal to -20.4 is -21, -21.0 is printed. Notice that even if we input a non-decimal number into the floor function, a double is returned. Since data precision is not hindered when you convert from an int data type to a double, Java will do it without throwing any errors. The floor function's primary use is rounding, all ready for you to use while coding.

Edit Me on GitHub!

Application Question

Math.floor(-5.5) returns