Home » Java programming language

Java StrictMath rint() Method with Example

StrictMath Class rint() method: Here, we are going to learn about the rint() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on January 05, 2020

StrictMath Class rint() method

  • rint() Method is available in java.lang package.
  • rint() 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.
  • rint() Method is a static method so it is accessible with the class name and if we try to access the method with the class object then we will not get any error.
  • rint() Method does not throw any exception.

Syntax:

    public static double rint(double d);

Parameter(s):

  • double d – represents a double type value.

Return value:

The return type of the method is double, it returns the double floating point number which will be equivalent to mathematical integer.

Note:

  • If we pass NaN, the method returns the NaN.
  • If we an infinity, the method returns the same (i.e. infinity).
  • If we pass an argument whose value after the decimal point is greater than 4, the method returns the value incremented by 1 before the decimal point.
  • If we pass zero, the method returns the same value with the same sign.

Example:

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

public class Rint {
    public static void main(String[] args) {
        // variable declarations
        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("StrictMath.rint(d1): " + StrictMath.rint(d1));

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

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

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

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

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

Output

StrictMath.rint(d1): -0.0
StrictMath.rint(d2): 0.0
StrictMath.rint(d3): -Infinity
StrictMath.rint(d4): Infinity
StrictMath.rint(d5): 1235.0
StrictMath.rint(d6): 1234.0


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.