Wednesday, July 3, 2024
HomeLanguagesJavaJava floor() method with Examples

Java floor() method with Examples

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 01:To show working of java.lang.Math.floor() method. 

java




// Java program to demonstrate working
// of java.lang.Math.floor() method
 
import java.lang.Math;
 
class Gfg {
 
    // Main 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

Example 02:To show the working of floor() with a positive double value.

Java




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        double number = 3.5; // double num-3.5
        double result = Math.floor(number);
        System.out.println(result); // Output: 3.0
    }
}


Output : 

3.0

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments