The Wayback Machine - https://web.archive.org/web/20221013174851/https://www.geeksforgeeks.org/java-floor-method-examples/
Skip to content
Related Articles

Related Articles

Java floor() method with Examples

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 08 Apr, 2018
View Discussion
Improve Article
Save Article

The java.lang.Math.floor() returns the double value that is less than or equal to the argument and is equal to the nearest mathematical integer.
Note:

  • If the argument is Integer, then the result is Integer.
  • If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.

public static double floor(double a)
a : the argument whose floor value is to be determined
Returns : This method returns the double value that is less than or equal
to the argument and is equal to the nearest mathematical integer.

Example :To show working of java.lang.Math.floor() method.




// Java program to demonstrate working
// of java.lang.Math.floor() method
import java.lang.Math;
  
class Gfg {
  
    // driver code
    public static void main(String args[])
    {
        double a = 4.3;
        double b = 1.0 / 0;
        double c = 0.0;
        double d = -0.0;
        double e = -2.3;
  
        System.out.println(Math.floor(a));
  
        // Input Infinity, Output Infinity
        System.out.println(Math.floor(b));
  
        // Input Positive Zero, Output Positive Zero
        System.out.println(Math.floor(c));
  
        // Input Negative Zero, Output Negative Zero
        System.out.println(Math.floor(d));
  
        // Input -2.3, Output -3.0
        // Nearest Integer(-3.0) < less than (-2.3)
        System.out.println(Math.floor(e));
    }
}


Output:

4.0
Infinity
0.0
-0.0
-3.0
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!