round() float in Java

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

Syntax:

int java.lang.Math.round(float a)

This method takes one argument of type float as its parameter. This method returns the closest int 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 Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.

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

Parameters: One parameter is required for this method.

a: a floating-point value to be rounded to an integer.

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

For Example:

Math.round(17.34) = > It returns 17.

Approach

Java

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

        float a = (float17.34;
        System.out.println(Math.round(a));
    }
}

Output:

17

No comments:

Post a Comment