Home » Java programming language

Java Math Class static double rint(double d) with example

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

Math Class static double rint(double d)

  • This method is available in java.lang package.
  • This method is used to return the double type value and if the value of the given argument after 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 decimal point is less than or equal to 4 then the same value before the decimal point is returned.
  • This is a static method, so it is accessible with the class name too.
  • The return type of this method is double, it returns the double floating point number which will be equivalent to mathematical integer.
  • In this method, we pass only one argument of double type.
  • This method does not throw any exception.

Syntax:

    public static double rint(double d){
    }

Parameter(s): double d – a double type of value to round to integer.

Return value:

The return type of this method is double, it returns the rounded (to integer) value.

Note:

  • If we pass "NaN" (Not a Number), it returns the same value "NaN".
  • If we pass infinity, it returns the same (i.e. infinity).
  • If we pass zero (-0 or 0), it returns the same value.

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

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

public class RintMethod {
    public static void main(String[] args) {
        // declaring the variable 
        double d1 = -0.0;
        double d2 = 0.0;
        double d3 = -1.0 / 0.0;
        double d4 = 1.0 / 0.0;
        double d5 = 1234.56;
        double d6 = 1234.12;

        // Here , we will get (-0.0) because we are passing parameter 
        // whose value is (-0.0)
        System.out.println("Math.rint(d1): " + Math.rint(d1));

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

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

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

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

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

Output

E:\Programs>javac RintMethod.java

E:\Programs>java RintMethod
Math.rint(d1): -0.0
Math.rint(d2): 0.0
Math.rint(d3): -Infinity
Math.rint(d4): Infinity
Math.rint(d5): 1235.0
Math.rint(d6): 1234.0





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.