Java Math.round Function

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

Syntax

The basic syntax of the math.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 positive or negative, it will return the nearest value.
  • If the number argument is not a number, it will return Zero.

This 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 of type Int.

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

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

The following round double function will accept the positive or negative double value and returns the closest math integer 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 this 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); 
 }
}
Java math.round Function 1

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.

In the last two lines, we used the Function directly on Negative double values.

Java Math.round on Array example

In this 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 math function.

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(Math.round(myArray[i]));
		}
	}
}

math round function output

-11
41
44
-13
-66
3

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

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

Next, We used Java For Loop to iterate the Array. Within the For Loop, we initialized the i 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( 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 program, we are going to declare an ArrayList of double type and find the closest values of list elements.

In this example, we used the For Loop to iterate the double values in ArrayList. If you observe the code snippet, we used the Math round Function directly inside the System.out.format statement.

Here, the compiler will call the long method to find the corresponding closest values and prints the output.

package MathFunctions;

import java.util.ArrayList;

public class RoundMethodOnList {
 public static void main(String[] args) {
 
 ArrayList<Double> myList = new ArrayList<Double>(5);
 myList.add(-15.289);
 myList.add(-15.65);
 myList.add(25.986);
 myList.add(40.499);
 myList.add(40.589);
 
 for (double x : myList) {
 System.out.println(Math.round(x));
 }
 }
}

output

-15
-16
26
40
41

Comments are closed.