rint() in Java (Rounding to int)


rint() is an inbuilt method in Java which is used to round of the floating-point argument to an integer value (in floating-point format).

Syntax:

Math.rint(double n)

Parameters:
The rint() function takes a mandatory single argument value to round.

Returns:
A double value is returned. This method returns the integer that is closest in value to the argument.

Code#1: Program demonstrating the use of rint() function

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for implementation of
// rint() method
import java.util.*;
class GFG {
  
    // Driver Code
    public static void main(String args[])
    {
        double x = 12.6;
        double y = 12.2;
  
        // rint() method use to return the 
        // closest value to the argument
        double rintx = Math.rint(x);
        double rinty = Math.rint(y);
  
        // Prints the rounded numbers
        System.out.println(rintx);
        System.out.println(rinty);
    }
}

chevron_right


Output:

13.0
12.0

Exception:
In case of 0.5 at decimal places it will round off to nearest even double value.

Code#2: Program explaining the exceptions

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for implementation of
// rint() method exception
import java.util.*;
class GFG {
  
    // Driver Code
    public static void main(String args[])
    {
        double x = 1.5;
        double y = 2.5;
  
        // rounds upto 2 which is the nearest
        // even double value
        double rintx = Math.rint(x);
        double rinty = Math.rint(y);
  
        System.out.println(rintx);
        System.out.println(rinty);
    }
}

chevron_right


Output:

2.0
2.0


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.