The Math.round()
method in Java is used to round a floating-point number (either float
or double
) to the nearest integer. It returns the result as a long
value for double
inputs and an int
value for float
inputs.
Syntax:
public static long round(double a);
public static int round(float a);
round(double a)
: Rounds adouble
value to the nearest integer. The result is returned as along
.round(float a)
: Rounds afloat
value to the nearest integer. The result is returned as anint
.
Rounding Logic:
- If the fractional part of the number is less than 0.5, it rounds down (towards zero).
- If the fractional part of the number is greater than or equal to 0.5, it rounds up (away from zero).
Examples:
Example 1: Rounding a double
value
public class RoundExample {
public static void main(String[] args) {
double value1 = 4.3;
double value2 = 4.7;
double value3 = -4.3;
double value4 = -4.7;
System.out.println("Rounded value of 4.3: " + Math.round(value1)); // Output: 4
System.out.println("Rounded value of 4.7: " + Math.round(value2)); // Output: 5
System.out.println("Rounded value of -4.3: " + Math.round(value3)); // Output: -4
System.out.println("Rounded value of -4.7: " + Math.round(value4)); // Output: -5
}
}
Explanation:
4.3
rounds down to4
because the fractional part (0.3) is less than 0.5.4.7
rounds up to5
because the fractional part (0.7) is greater than or equal to 0.5.- Similarly,
-4.3
rounds to-4
(towards zero), and-4.7
rounds to-5
(away from zero).
Example 2: Rounding a float
value
public class RoundFloatExample {
public static void main(String[] args) {
float value1 = 5.4f;
float value2 = 5.6f;
float value3 = -5.4f;
float value4 = -5.6f;
System.out.println("Rounded value of 5.4: " + Math.round(value1)); // Output: 5
System.out.println("Rounded value of 5.6: " + Math.round(value2)); // Output: 6
System.out.println("Rounded value of -5.4: " + Math.round(value3)); // Output: -5
System.out.println("Rounded value of -5.6: " + Math.round(value4)); // Output: -6
}
}
Explanation:
- The rounding behavior for
float
values is the same as fordouble
. The method rounds5.4
to5
and5.6
to6
. The same logic applies to negative values.
Example 3: Rounding a double
with larger numbers
public class RoundLargeNumberExample {
public static void main(String[] args) {
double largeValue1 = 1234567.89;
double largeValue2 = 1234567.51;
System.out.println("Rounded value of 1234567.89: " + Math.round(largeValue1)); // Output: 1234568
System.out.println("Rounded value of 1234567.51: " + Math.round(largeValue2)); // Output: 1234568
}
}
Explanation:
1234567.89
rounds up to1234568
because the fractional part.89
is greater than 0.5.1234567.51
rounds up to1234568
as well, since.51
is greater than or equal to 0.5.
Example 4: Using Math.round()
in a real-world context
public class RoundSalaryExample {
public static void main(String[] args) {
double salary = 12345.67;
System.out.println("Salary after rounding: " + Math.round(salary)); // Output: 12346
}
}
Explanation:
- A salary value of
12345.67
is rounded to12346
, as.67
is greater than 0.5.
Important Notes:
- Rounding a positive number: If the fractional part is
.5
or greater, it will round up. Otherwise, it rounds down. - Rounding a negative number: For negative values, the number rounds away from zero if the fractional part is
.5
or greater, and towards zero if it’s less than.5
. - Return Type:
Math.round()
forfloat
returns anint
, while fordouble
, it returns along
.
Summary:
- The
Math.round()
method is useful when you need to round a floating-point number to the nearest integer. - The method rounds up if the decimal part is
>= 0.5
, and rounds down if it’s< 0.5
. - It returns
long
fordouble
values andint
forfloat
values.