# Java rint Function

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

## Java rint Function syntax

The basic syntax of the Math.rint Function in Java Programming language is as 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, Math.rint function will return the nearest value.
• If the number argument is not a number (NaN) or an infinity or positive zero or negative zero, the Java Math.rint function will return the argument value as a result.

## Java Math.rint Function Example

The Java Math.rint function rounds the double value to the nearest integer. In this Java program, We are going to 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));
}
}
```

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

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

Next, We used the Math.rint 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 rint 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 type and find the closest (rounded) values of array elements.

```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 Result of Array Element = " + Math.rint(rintArray[i]));
}
}
}```
``````Math.rint Result of Array Element = -19.0
Math.rint Result of Array Element = 111.0
Math.rint Result of Array Element = 144.0
Math.rint Result of Array Element = -121.0
Math.rint Result of Array Element = -606.0
Math.rint Result of Array Element = 38.0``````

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

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

`for (int i = 0; i < rintArray.length; i++) {`

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

`System.out.println("Math.rint Result of Array Element = " + Math.rint(rintArray[i]));`

## 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 are going to declare an ArrayList of double type and find the closet values of list elements.

```package MathFunctions;

import java.util.ArrayList;

public class RintMethodOnList {
public static void main(String[] args) {

ArrayList<Double> myList = new ArrayList<Double>(5);

for (double x : myList) {
System.out.println("Math.rint Result of ArrayList =  " + Math.rint(x));
}
}
}```
``````Math.rint Result of ArrayList =  -2.0
Math.rint Result of ArrayList =  -3.0
Math.rint Result of ArrayList =  3.0
Math.rint Result of ArrayList =  59.0
Math.rint Result of ArrayList =  34.0``````

First, we used the For Loop to iterate the double values in an ArrayList.

`for (double x : myList) {`

Here, we used the Math.rint Function directly inside the System.out.format statement. Here, the compiler will call the math.rint method ( static double rint(double x) ) to find the corresponding closest values and prints the output.

`System.out.println("Math.rint Result of ArrayList =  " + Math.rint(x));`