Java Math rint() Method

Java Math rint() Method

Java Math Class

Here, in the page we will discuss about the math rint() Method in java.
Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), abs() etc.The java.lang.Math class contains various methods for performing basic numeric operations

Java Math incrementExact() Method :

The rint() method in Java is a static method that belongs to the Math class. It returns the closest double to the argument, with ties rounding to the nearest even value.
 

Special cases to consider when using the rint() method :

  • If the argument is NaN (not a number), the method returns NaN.
  • If the argument is positive infinity, the method returns positive infinity.
  • If the argument is negative infinity, the method returns negative infinity

Syntax  :

public static double rint(double a)

Example 1 :  

Run
public class Main {
    public static void main(String[] args) {
        double number1 = 3.4;
        double roundedNumber1 = Math.rint(number1);

        double number2 = 4.5;
        double roundedNumber2 = Math.rint(number2);

        double number3 = 5.5;
        double roundedNumber3 = Math.rint(number3);

        System.out.println("The rounded number is: " + roundedNumber1);
        System.out.println("The rounded number is: " + roundedNumber2);
        System.out.println("The rounded number is: " + roundedNumber3);
    }
}

Output :

The rounded number is: 3.0
The rounded number is: 4.0
The rounded number is: 6.0

Explanation :

This program uses the rint() method to round three different numbers to the nearest integer value. The rint() method rounds the value of the argument to the nearest integer, with ties rounding to the nearest even value. In this example, the value of number1 is 3.4, which is closer to 3 than it is to 4. Therefore, the rint() method returns 3.0. The value of number2 is 4.5, which is closer to 4 than it is to 5. Therefore, the rint() method returns 4.0. The value of number3 is 5.5, which is exactly halfway between 5 and 6. Therefore, the rint() method rounds up to 6.0, because it rounds ties to the nearest even value.

Example 2:  

Run
public class Main {
    public static void main(String[] args) {
        double number1 = Double.NaN;
        double roundedNumber1 = Math.rint(number1);

        double number2 = Double.POSITIVE_INFINITY;
        double roundedNumber2 = Math.rint(number2);

        double number3 = Double.NEGATIVE_INFINITY;
        double roundedNumber3 = Math.rint(number3);

        System.out.println("The rounded number is: " + roundedNumber1);
        System.out.println("The rounded number is: " + roundedNumber2);
        System.out.println("The rounded number is: " + roundedNumber3);
    }
}

Output :

The rounded number is: NaN
The rounded number is: Infinity
The rounded number is: -Infinity

Explanation :

This program uses the rint() method to round three special values: NaN, POSITIVE_INFINITY, and NEGATIVE_INFINITY. The rint() method handles these special cases as follows:

  • If the argument is NaN (not a number), the method returns NaN.
  • If the argument is positive infinity, the method returns positive infinity.
  • If the argument is negative infinity, the method returns negative infinity.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription