Java Math floor() Method

java math floor() math

Java Math Class

Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), abs() etc.
The java.lang.Math class contains various methods for performing basic numeric operations

Here, in the page we will discuss about the math floor() Method in java.

Java Math floor() Method :

This function returns the largest (closest to positive infinity) double value that is both less than or equal to the argument and equal to an integer in mathematics.

Special Points :

  • The result is the same as the argument if the argument’s value is already an integer in mathematics.
  • The argument and the result are identical if the argument is NaN, an infinity, a positive zero, or a negative zero.

Syntax  :

public static double floor(double a)

Example 1 :  

Run

public class Main{
    public static void main(String[] args) {
        
        //declaring variables
        double x = 50.3;
        double y = 75.9;
        double z = -55.2;
        double e = -30.1;
        
        //printing out the results of the floor method
        System.out.println("Math.floor(" + x + ") = " + Math.floor(x));
        System.out.println("Math.floor(" + y + ") = " + Math.floor(y));
        System.out.println("Math.floor(" + z + ") = " + Math.floor(z));
        System.out.println("Math.floor(" + e + ") = " + Math.floor(e));
    }
}

Output :

Math.floor(50.3) = 50.0
Math.floor(75.9) = 75.0
Math.floor(-55.2) = -56.0
Math.floor(-30.1) = -31.0

Example 2  :  

Run
public class Main{
    public static void main(String[] args) {
        
        //declaring variables
        double x = 50.3/0;
        double y = 0.0;
        double z = -0.0;
        double e = -30.1;
        
        //pringing output for infinite input
        System.out.println("Math.floor(" + x + ") = " + Math.floor(x));

        //printing output for positive and negative input
        System.out.println("Math.floor(" + y + ") = " + Math.floor(y));
        System.out.println("Math.floor(" + z + ") = " + Math.floor(z));

        //printing output for negative input
        System.out.println("Math.floor(" + e + ") = " + Math.floor(e));
    }
}

Output :

Math.floor(50.3/0) = Infinity
Math.floor(0.0) = 0.0
Math.floor(-0.0) = -0.0
Math.floor(-30.1) = -31.0

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription