Home » Java programming language

Java Math Class static long round(double d) with example

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

Math Class static long round(double d)

  • This method is available in java.lang package.
  • This method is used to return the closest long value to the given argument.
  • This is a static method, it is accessible with the class name too.
  • The return type of this method is long, it returns the long type number which will be converted from double floating to long by adding 1/2 of the given argument.
  • In this method, we pass only one parameter of a double type value.
  • If the value of the given argument after the decimal point is greater than 4 then the value is incremented by 1 before the decimal point is returned else if the value of the given argument after the decimal point is less than or equal to 4 then the same value before the decimal point is returned.
  • This method does not throw any exception.

Syntax:

    public static long round(double d){
    }

Parameter(s): d – a double value whose closest to long value to be found.

Note:

  • If we pass "NaN" (Not a Number), it returns 0.
  • If we pass negative infinity, it returns the "Long.MIN_VALUE".
  • If we pass positive infinity, it returns the "Long.MAX_VALUE".
  • If we pass the value which is less than or equal to "Long.MIN_VALUE", it returns "Long.MIN_VALUE".
  • If we pass the value which is greater than or equal to "Long.MAX_VALUE", it returns "Long.MAX_VALUE".

Return value:

The return type of this method is long, it returns a long value which is the closest to long value of given parameter.

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

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

public class RoundMethod {
    public static void main(String[] args) {
        // declaring the variables
        double d1 = -1.0 / 0.0;
        double d2 = 1.0 / 0.0;
        double d3 = 1234.56;
        double d4 = 1234.42;

        // Here , we will get (Long.MIN_VALUE) and we are 
        // passing parameter whose value is (-Infinity)
        System.out.println("Math.round(d1): " + Math.round(d1));

        // Here , we will get (Long.MAX_VALUE) and we are 
        // passing parameter whose value is (Infinity)
        System.out.println("Math.round(d2): " + Math.round(d2));

        // Here , we will get (1235) and we are 
        // passing parameter whose value is (1234.56)
        System.out.println("Math.round(d3): " + Math.round(d3));

        // Here , we will get (1234) and we are 
        // passing parameter whose value is (1234.12)
        System.out.println("Math.round(d4): " + Math.round(d4));
    }
}

Output

E:\Programs>javac RoundMethod.java

E:\Programs>java RoundMethod
Math.round(d1): -9223372036854775808
Math.round(d2): 9223372036854775807
Math.round(d3): 1235
Math.round(d4): 1234


Comments and Discussions!

Load comments ↻





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