Java rint() Method: Usage and Examples
Table of Content:
Description
This Java tutorial shows how to use the rint(double a) method of Math class under java.lang package. This method returns the double value that is closest in value to the argument and is equal to a mathematical integer. If two double values that are mathematical integers are equally close, the result is the integer value that is even.
The method rint returns the integer that is closest in value to the argument.
Syntax
double rint(double d)
Parameters
Here is the detail of parameters ?
-
d ? it accepts a double value as a parameter.
Return Value
-
This method returns the integer that is closest in value to the argument. Returned as a double.
Example
public class MathRintMethod { public static void main(String args[]) { double d = 120.675; double e = 120.500; double f = 120.200; System.out.println(Math.rint(d)); System.out.println(Math.rint(e)); System.out.println(Math.rint(f)); } }
Output
121.0 120.0 120.0 Press any key to continue . . .
Example
This java example source code demonstrates the use of rint(double a) method of Math class. Basically we just get the rounded value of a double variable and then print the results.
import static java.lang.System.*; import java.util.Scanner; /* * This example source code demonstrates the use of * rint(double a) method of Math class * Get the rounded value of the user input */ public class MathRound { public static void main(String[] args) { // ask for user input out.print("Enter a value:"); Scanner scan = new Scanner(System.in); // use scanner to get user console input double value = scan.nextDouble(); // get the rounded value double roundValue = Math.rint(value); out.println("floor of "+value+" = "+roundValue); // close the scanner object to avoid memory leak scan.close(); } }
Output
Running the rint(double a) method example source code of Math class will give you the following output.
Enter a value:1365.4561 floor of 1365.4561 = 1365.0 Press any key to continue . . .