Java

How to Use the Java Math.round() Method

While dealing with mathematical computations in Java, there can be instances where the developer needs to fetch a rounded integer instead of a float. For instance, rounding off the numbers comprising infinite decimal points affects the processing speed and consumes memory as well. In such situations, the “Math.round()” method in Java assists in omitting ambiguity.

This blog will discuss the usage of the “Math.round()” method in Java.

How to Use the Java “Math.round()” Method?

The “Math.round()” method is used to round off the number specified as its parameter to its nearest up or down integer.

Syntax

public static int round(float x)

 
In the above syntax, “x” corresponds to the float or double that needs to be rounded off to the nearest integer.

Example 1: Utilization of “Math.round()” Method to Round Off Float Variable

In this example, this method can be utilized to round off two identical numbers to their corresponding nearest integers:

double number1 = 53.65;
double number2 = 53.25;
System.out.println("The rounded number is: " +Math.round(number1));
System.out.println("The rounded number is: " +Math.round(number2));

 
In the above code-snippet:

    • Initialize the stated two float values.
    • After that, apply the “Math.round()” method upon both the floats to round them to the nearest up and down integers, respectively.

Output


 

In the above output, the difference between both the rounded off identical numbers can be observed.

Example 2: Utilization of “Math.round()” Method to Compute Positive and Negative Infinity

In this particular example, the discussed method can be applied to return the maximum and minimum values of the long variables:

double positiveInfinity = Double.POSITIVE_INFINITY;
double negativeInfinity = Double.NEGATIVE_INFINITY;
System.out.println(Math.round(positiveInfinity));
System.out.println(Math.round(negativeInfinity));

 
In the above lines of code, apply the following steps:

    • Firstly, specify the positive and negative infinities, respectively in order to round them.
    • Now, apply the “Math.round()” method upon both the positive and negative infinities to fetch the values of maximum and minimum longs, respectively.

Output


 

In the above output, it is evident that upon rounding off the positive and negative infinities, the extreme values of long, i.e., max and min have been returned.

Conclusion

The “Math.round()” method in Java is utilized to round off the number specified as its parameter to its nearest up or down integer. In the former example, it can be used to round off the specified floats into the nearest integer. In the latter illustration, it can be applied to fetch the maximum and minimum values of long. This blog elaborated on the usage of the “Math.round()” method in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.