Java rint Function

The Java rint Function is one of the Math Library functions, which rounds the specified expression or an individual number to the nearest mathematical integer. This article will show how to use Java Math.rint function with an example.

Java rint Function Syntax

The basic syntax of the Math.rint Function in Java Programming language is shown below.

static double rint(double number); //Return Type is Double

// In order to use in program: 
Math.rint(double number);

Number: It can be a double value or a valid numerical expression.

  • If the number argument is a positive or negative number, this function will return the nearest value.
  • If the number argument is not a number (NaN) or an infinity or positive zero or negative zero, it will return the argument value as a result.

Java Math.rint Function Example

The Math.rint function rounds the double value to the nearest integer. In this Java program, we will round both the positive and negative values and display the output.

package MathFunctions;

public class RintMethod {
	public static void main(String[] args) {
		double a = Math.rint(140.456 - 34.9865 - 4.52);
		System.out.println("Math.rint Result = " + a);

		System.out.println("\nMath.rint Result of Positive Number: " + Math.rint(0.25));
		System.out.println("Math.rint Result of Positive Number: " + Math.rint(12.05));
		System.out.println("Math.rint Result of Positive Number: " + Math.rint(12.85));
		
		System.out.println("\nMath.rint Result of Negative Number: " + Math.rint(-10.85));
		System.out.println("RMath.rint Result of Negative Number: " + Math.rint(-10.25));
	}
}
Java rint Function 1

Analysis

First, We declared a variable of type Double and performed the Java rint function directly on the expression.

double a = Math.rint(140.456 - 34.9865 - 4.52);
System.out.println("Math.rint Result = " + a);

Next, We used this Function directly on Positive double values.

System.out.println("\nMath.rint Result of Positive Number: " + Math.rint(0.25));
System.out.println("Math.rint Result of Positive Number: " + Math.rint(12.05));
System.out.println("Math.rint Result of Positive Number: " + Math.rint(12.85));

Next, We used the Math function directly on Negative double values.

System.out.println("\nMath.rint Result of Negative Number: " + Math.rint(-10.85));
System.out.println("RMath.rint Result of Negative Number: " + Math.rint(-10.25));

Java Math.rint on Array example

In this Java program, we find the rounded values of bulk data. Here, we are going to declare an array of double types and find the closest (rounded) values of array elements.

We used the For Loop to iterate the Java Array. Within the For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < rintArray.length).

TIP: Array.length finds the length of a given array.

Next, we used the Math Function directly inside the System.out.format statement. The Java compiler will call the static double rint(double number) to find the corresponding closet values and print the output.

Code

package MathFunctions;

public class RintMethodOnArrays {
	public static void main(String[] args) {
		
		double [] rintArray = {-19.25, 110.98, 144.21, -120.59, -605.87, 38.4897};

		for (int i = 0; i < rintArray.length; i++) {
			System.out.println(Math.rint(rintArray[i]));
		}
	}
}
-19.0
111.0
144.0
-121.0
-606.0
38.0

Java Math.rint function on Arraylist example

To find the closet value of a single item, then use the Math.rint(myArray[index_position]). In this Java program, we will declare an ArrayList of double type and find the closest values of list elements.

First, we used the For Loop to iterate the double values in an ArrayList. Next, we used the Math.rint Function directly inside the System.out.format statement. The compiler will call the math method ( static double rint(double x) ) to find the corresponding closest values and print the output.

package MathFunctions;

import java.util.ArrayList;

public class RintMethodOnList {
	public static void main(String[] args) {
		
		ArrayList<Double> myList = new ArrayList<Double>(5);
		myList.add(-2.289);
		myList.add(-2.65);
		myList.add(Math.PI);
		myList.add(59.05);
		myList.add(33.568);
		
		for (double x : myList) {
			System.out.println(Math.rint(x));
		}
	}
}
-2.0
-3.0
3.0
59.0
34.0