java.lang.Math rint(double a)

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.

This method in simple language is simply here to round the double method argument.

Method Syntax :

public static double rint(double a)

Parameter Input :

DataType Parameter Description
double a a double value

Method Returns :

The rint(double a) method simply returns the closest floating-point value to a that is equal to a mathematical integer.

Compatibility Version :

Requires Java 1.0 and up

Exception :

N/A

Java Code 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.

package com.javatutorialhq.java.examples;

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();

	}

}

Sample Output :

Running the rint(double a) method example source code of Math class will give you the following output

Enter a value:123.501
floor of 123.501 = 124.0

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :