round() double in Java

round(): This method is available in the Math class of Java.

Syntax:

long java.lang.Math.round(double a)

This method takes one argument of type double as its parameter. This method returns the closest long to the argument, with ties rounding to positive infinity.

Special cases:

1. If the argument is NaN, the result is 0.

2. If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.

3. If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.

Parameters: One parameter is required for this method.

a: a floating-point value to be rounded to a long.

Returns: the value of the argument rounded to the nearest long value.

For Example:

Math.round(199.892) = > It returns 200.

Approach

Java

public class RoundDouble {
    public static void main(String[] args) {

        double a = 199.892;
        System.out.println(Math.round(a));

    }
}

Output:

200

No comments:

Post a Comment