In Java, Math.rint() is an inbuilt method that is used to round off the floating-point argument to an integer value (in floating-point format).
Syntax of rint() Method
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.
The complexity of the method
Time Complexity: O(1)
Space Complexity: O(1)
Example of Math rint() Method
Example 1:
Program demonstrating the use of rint() function.
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
double x = 12.6;
double y = 12.2;
double rintx = Math.rint(x);
double rinty = Math.rint(y);
System.out.println(rintx);
System.out.println(rinty);
}
}
|
Exception: In case of 0.5 at decimal places it will round off to nearest even double value.
Example 2:
Program explaining the exceptions
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
double x = 1.5;
double y = 2.5;
double rintx = Math.rint(x);
double rinty = Math.rint(y);
System.out.println(rintx);
System.out.println(rinty);
}
}
|
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
30 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...