Java .rint()
Published Sep 20, 2022
Contribute to Docs
The Math.rint() returns a double that is rounded to the closest whole integer.
Syntax
Math.rint(num)
The .rint() method takes a double num parameter and returns a double.
Some edge cases for the .rint() method include the following:
- If the value of
numis passed an integer, then it is returned as a double. - If the value of
numisNaNor positive/negative infinity, then the value ofnumis returned. - If the value of
numis positive/negative zero, then0.0is returned.
Example
The following example demonstrates the .rint() method:
public class Test {public static void main(String[] args) {System.out.println(Math.rint(8.75));// When the whole number is odd and the fractional part is .5, round up.System.out.println(Math.rint(3.5));// When the whole number is even and the fractional part is .5, round down.System.out.println(Math.rint(6.5));// Edge casesSystem.out.println(Math.rint(-0));System.out.println(Math.rint(Double.NaN));System.out.println(Math.rint(Double.POSITIVE_INFINITY));System.out.println(Math.rint(Double.NEGATIVE_INFINITY));}}
This will produce the following output:
9.04.06.00.0NaNInfinity-Infinity
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours