Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaStrictMath round() method in Java

StrictMath round() method in Java

round(double num)

The round(double num) is the inbuilt method of StrictMath class in Java which is used to obtain the closest long to the given argument num. It returns the rounded value to an integer by adding 1/2, and taking the floor of the obtained result, and after that casting the result to type long. It returns 0 when the argument is NaN and the value equal to the value of Long.MIN_VALUE when the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE. Also it returns the value equal to the value of Long.MAX_VALUE when the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE. Syntax:

public static long round(double num)

Parameters: The method accepts single parameter num of double type which is to be rounded. Return Value: The method returns the rounded value to the nearest long value of the value passed as parameter to this method. Examples :

Input: num = 34.14
Output: 34

Below programs illustrate the round() method: Program 1: 

java




// Java program to illustrate the
// java.lang.StrictMath.round()
 
import java.lang.*;
 
public class Geeks {
    public static void main(String[] args)
    {
 
        // Get a double number
        double num1 = 62.9;
 
        // Round off the double number using round() method
        long round_Value = StrictMath.round(num1);
 
        // Print the result
        System.out.println(" The round off value of "
                           + num1 + " = " + round_Value);
 
        // Get a double number
        double num2 = 87.1;
 
        // Round off the double number using round() method
        round_Value = StrictMath.round(num2);
 
        // Print the result
        System.out.println("The round off value of "
                           + num2 + " = " + round_Value);
    }
}


Output:

The round off value of 62.9 = 63
The round off value of 87.1 = 87

round(float num)

The round(float num) is the inbuilt method of StrictMath class in Java which we use to obtain the closest int to the given argument num. It returns the rounded value to an integer by adding 1/2, and taking the floor of the obtained result, and after that casting the result to type int.It returns 0 when the argument is NaN, it returns the value equal to the value of Integer.MIN_VALUE when the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE. It returns the value equal to the value of Integer.MAX_VALUE when the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE. Syntax:

public static int round(float num)

Parameters: The method accepts single parameter num which is of float type. Return Value: The method returns the value of the argument that is rounded to the nearest int value. Examples :

Input: num = 72.15f
Output: 72

Below programs illustrate the java.lang.StrictMath.round() method: Program 1: 

java




// Java program to illustrate the
// java.lang.StrictMath.round()
 
import java.lang.*;
 
public class Geeks {
    public static void main(String[] args)
    {
 
        // Get a float number
        float num1 = 87.1f;
 
        // Round off the float number using round() method
        int round_Value = StrictMath.round(num1);
 
        // Print the result
        System.out.println(" The round off value of "
                           + num1 + " = " + round_Value);
 
        // Get a float number
        float num2 = 92.9f;
 
        // Round off the float number using round() method
        round_Value = StrictMath.round(num2);
 
        // Print the result
        System.out.println("The round off value of "
                           + num2 + " = " + round_Value);
    }
}


Output:

The round off value of 87.1 = 87
The round off value of 92.9 = 93
RELATED ARTICLES

Most Popular

Recent Comments