# Java Math.round Function

The Java Math.round Function is one of the Java Math functions used to round the specified expression or an individual number to the nearest integer.

## Java Math.round Syntax

The basic syntax of the round Function in Java Programming language is as shown below.

`Math.round(data_type number);`

Number: It can be a number or a valid numerical expression.

• If the number argument is a positive or negative number, the Math.round function will return the nearest value.
• If the number argument is not a number, the Java Math.round function will return Zero.

Java Programming provides two different functions to round the specified value. The following Java math.round function will accept positive or negative float value as an argument and returns the closest mathematical integer value of type Int.

```static int round(float number); //Return Type is Integer

// In order to use in program:
Math.round(float number);```

The following Java round double function will accept the positive or negative double value and returns the closest math integer value of type long.

```static long round(double number); //Return Type is Long

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

## Java Math.round Function Example

We use the Java Round function to round the values of both positive and negative values and display the output.

```package MathFunctions;

public class RoundMethod {
public static void main(String[] args) {
double a = Math.round(10.9666 - 14.9865 + 154.9852);

System.out.println("Rounded value of Positive Number: " + Math.round(10.25));
System.out.println("Rounded value of Positive Number: " + Math.round(10.95));

System.out.println("\nRounded value of Negative Number: " + Math.round(-20.85));
System.out.println("Rounded value of Negative Number: " + Math.round(-20.25));

System.out.println("\nRounded value = " + a);
}
}```

First, we declared a variable of type Double and performed the round function directly on expression.

`double a = Math.round(10.9666 - 14.9865 + 154.9852);`

Math.round(10.9666 – 14.9865 + 154.9852)

==> Math.round (150.9653) ==> 151

Next, we used the round Math function directly on Positive double values.

```System.out.println("Rounded value of Positive Number: " + Math.round(10.25));
System.out.println("Rounded value of Positive Number: " + Math.round(10.95));```

Next, we used the Java Math.round Function directly on Negative double values.

```System.out.println("\nRounded value of Negative Number: " + Math.round(-20.85));
System.out.println("Rounded value of Negative Number: " + Math.round(-20.25));```

## Java Math.round 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 using java math.round.

```package MathFunctions;

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

double [] myArray = {-10.69, 40.98, 44.21, -12.59, -65.87, 3.4897};

for (int i = 0; i < myArray.length; i++) {
System.out.println("Rounded value of Array Element = " + Math.round(myArray[i]));
}
}
}```

Java math round function output

``````Rounded value of Array Element = -11
Rounded value of Array Element = 41
Rounded value of Array Element = 44
Rounded value of Array Element = -13
Rounded value of Array Element = -66
Rounded value of Array Element = 3``````

Within this Java round example, we declared an Array of double type and assigned some random values.

`double [] myArray = {-10.69, 40.98, 44.21, -12.59, -65.87, 3.4897};`

Next, We used the Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0.

Next, compiler will check for the condition (i < myArray.length). As along the condition is True statement inside the for loop executed.

TIP: myArray.length finds the length of the Java array.

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

The following statements will print the output. If you observe the code snippet, we used the round Function directly inside System.out.format statement.

Here, the compiler will call the Java Math.round method ( static long round(double number) ) to find the corresponding closet (rounded) values.

`System.out.println("Rounded value of Array Element = " + Math.round(myArray[i]));`

NOTE: To find the closet (rounded) value of a single item, then use: Math.round(myArray[index_position])

## Java Math.round function on Arraylist example

In this Java math.round program, we are going to declare an ArrayList of double type and find the closet (rounded) values of list elements.

```package MathFunctions;

import java.util.ArrayList;

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

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

for (double x : myList) {
System.out.println("Rounded Value of ArrayList =  " + Math.round(x));
}
}
}```

Java math round function output

``````Rounded Value of ArrayList =  -15
Rounded Value of ArrayList =  -16
Rounded Value of ArrayList =  26
Rounded Value of ArrayList =  40
Rounded Value of ArrayList =  41``````

In this math.round example, we used the For Loop to iterate the double values in ArrayList

`for (double x : myList) {`

If you observe the code snippet, we used the Java Math round Function directly inside the System.out.format statement.

Here, the compiler will call the math.round method ( static long round(double x) ) to find the corresponding closest values, and prints the output.

`System.out.println("Rounded Value of ArrayList =  " + Math.round(x));`