Java Math.round() syntax
java.lang.Math.round() is a math method that returns the nearest long or integer to its argument. The result of Java Math round() is rounded to an integer by adding 1/2 and taking the floor of the result after adding 1/2. After this operation, the number is cast to a long or int type. The syntax of the round() method is:
Math.round(value)
round() just like the most of Math class methods is static. Value argument could be float or double. The method returns the nearest int (in case of float value) or long (in case of double value) number to the argument, with ties rounding to positive infinity.
Special cases of Math.round()
If the argument is NaN, the result will be 0.
If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result will be the value of Integer.MIN_VALUE.
If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.
Java Math.round() example
Let’s have a program and demonstrate the Math round() method with examples of different arguments, float and double.
public class MathExample {
//java.lang.Math.round() method example with float and double arguments
public static void main(String[] args) {
double e = 2.71828;
float pi = 3.1415f;
//Math.round() method: float turns to int
int intOfPi = Math.round(pi);
//Math.round() method: double turns to long
long intOfE = Math.round(e);
System.out.println("integer part of pi = " + intOfPi);
System.out.println("integer part of e = " + intOfE);
}
}
The output of this program is:
Parameters Accepted by Math.round()
The Math.round()
method is overloaded and can accept two different parameter types:
- float: A single-precision 32-bit floating-point value. This parameter type is used for rounding smaller decimal numbers, ensuring compatibility with float-based operations.
- double: A double-precision 64-bit floating-point value. This parameter type is used for rounding larger or more precise decimal numbers.
Both overloads of Math.round()
return the rounded result as an integer type, but there is a key distinction in the return type based on the input:
- For float inputs, the return type is
int
. - For double inputs, the return type is
long
.
Examples:
float floatValue = 5.6f;
int roundedFloat = Math.round(floatValue); // Returns 6 (int)
double doubleValue = 5.6;
long roundedDouble = Math.round(doubleValue); // Returns 6 (long)
Behavior of Math.round() with Different Data Types
The behavior of Math.round()
depends on the input data type:
- When a float is passed: The method rounds the value to the nearest integer using standard rounding rules (values ≥ 0.5 are rounded up). The result is cast to an
int
, which means it can only handle integer results within the range of a 32-bit signed integer. - When a double is passed: The method rounds the value to the nearest long integer using the same rounding rules. The result is cast to a
long
, allowing it to handle larger values compared to the float version.
Examples with Different Data Types:
float floatValue1 = 5.4f;
int roundedFloat1 = Math.round(floatValue1); // Returns 5
float floatValue2 = 5.5f;
int roundedFloat2 = Math.round(floatValue2); // Returns 6
double doubleValue1 = 12345.678;
long roundedDouble1 = Math.round(doubleValue1); // Returns 12346
double doubleValue2 = -123.45;
long roundedDouble2 = Math.round(doubleValue2); // Returns -123
Understanding the behavior based on data types is crucial for ensuring the correct application of Math.round()
, especially when working with mixed data types or large numerical values.
GO TO FULL VERSION