Home » Java programming language

Java Math Class static double floor(double d) with example

Java Math Class static double floor(double d) method: Here, we are going to learn about the static double floor(double d) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on August 22, 2019

Math Class static double floor(double d)

  • This method is available in java.lang package.
  • In this method if the value of the given positive argument after decimal point is 0 or greater than 0 so in that case it returns the same number before decimal point else if the value of the given negative argument after decimal point is greater than 0 so it returns (the same number +1) before decimal point.
  • This is a static method so this method is accessible with the class name too.
  • The return type of this method is double that means it returns the greatest floating-point value of the given argument and the argument value may be less than or equal to the given argument.
  • In this method, we pass only one parameter as an argument in the method of Math class.
  • This method does not throw any exception.

Syntax:

    public static double floor(double d){
    }

Parameter(s):

double d – A double value whose greatest floating-poin value to be found.

Note:

  • If we pass "NaN", it returns "NaN".
  • If we pass a positive infinity, it returns the same i.e. a positive infinity.
  • If we pass a negative infinity, it returns the same i.e. a negative infinity.
  • If we pass 0 (-0 or 0), it returns the same.

Return value:

The return type of this method is double, it returns the greatest floating-point value of the given value.

Java program to demonstrate example of floor(double d) method

// Java program to demonstrate the example of floor(double d) 
// method of Math Class

public class FloorMethod {
    public static void main(String[] args) {
        // Here we are declaring few variables
        double d1 = 7.0 / 0.0;
        double d2 = -7.0 / 0.0;
        double d3 = 0.0;
        double d4 = -0.0;
        double d5 = -123.1;
        double d6 = 123.456;

        // Display previous value of d1,d2,d3,d4,d5 and d6 
        System.out.println(" Before implementing floor() so the value of d1 is :" + d1);
        System.out.println(" Before implementing floor() so the value of d2 is :" + d2);
        System.out.println(" Before implementing floor() so the value of d3 is :" + d3);
        System.out.println(" Before implementing floor() so the value of d4 is :" + d4);
        System.out.println(" Before implementing floor() so the value of d4 is :" + d5);
        System.out.println(" Before implementing floor() so the value of d4 is :" + d6);

        // Here , we will get (Infinity) because we are passing parameter 
        // whose value is (infinity)
        System.out.println("After implementing floor() so the value of d1 is :" + Math.floor(d1));

        // Here , we will get (-Infinity) because we are passing parameter 
        // whose value is (-infinity)
        System.out.println("After implementing floor() so the value of d2 is :" + Math.floor(d2));

        // Here , we will get (0.0) because we are passing parameter 
        // whose value is (0.0)
        System.out.println("After implementing floor() so the value of d3 is :" + Math.floor(d3));

        // Here , we will get (-0.0) because we are passing parameter 
        // whose value is (-0.0)
        System.out.println("After implementing floor() so the value of d4 is :" + Math.floor(d4));

        // Here , we will get (-124.0) because we are passing parameter 
        // whose value is (-123.1)
        System.out.println("After implementing floor() so the value of d5 is :" + Math.floor(d5));

        // Here , we will get (123.0) because we are passing parameter 
        // whose value is (123.456)
        System.out.println("After implementing floor() so the value of d6 is :" + Math.floor(d6));
    }
}

Output

E:\Programs>javac FloorMethod.java

E:\Programs>java FloorMethod
Before implementing floor() so the value of d1 is :Infinity
Before implementing floor() so the value of d2 is :-Infinity
Before implementing floor() so the value of d3 is :0.0
Before implementing floor() so the value of d4 is :-0.0
Before implementing floor() so the value of d4 is :-123.1
Before implementing floor() so the value of d4 is :123.456

After implementing floor() so the value of d1 is :Infinity
After implementing floor() so the value of d2 is :-Infinity
After implementing floor() so the value of d3 is :0.0
After implementing floor() so the value of d4 is :-0.0
After implementing floor() so the value of d5 is :-124.0
After implementing floor() so the value of d6 is :123.0


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.