The Math.round() method rounds a floating-point number to the nearest integer, returning an int (for float) or a long (for double).
Tie-Breaking Toward Positive Infinity: Defined as floor(x + 0.5), halfway values (.5) always round upward toward +Infinity. For strict downward or upward rounding, see Math.floor() and Math.ceil(). For random number scaling, explore Math.random() and Math.abs().
Unlike Math.floor() and Math.ceil() (which return double), Math.round() returns an integer primitive:
// Two overloaded signatures of Math.round() in java.lang.Math
public static int round(float a)
public static long round(double a)
| Overload Signature | Return Type | Mathematical Definition & Overflow Clamp |
|---|---|---|
Math.round(float a) |
int (32-bit) |
Computes (int) Math.floor(a + 0.5f); clamps to Integer.MIN_VALUE..MAX_VALUE. |
Math.round(double a) |
long (64-bit) |
Computes (long) Math.floor(a + 0.5d); clamps to Long.MIN_VALUE..MAX_VALUE. |
Fractional portions below .5 round toward the lower integer; portions above .5 round toward the higher magnitude:
// Positive and negative decimal rounding
System.out.println(Math.round(12.34)); // 12 (long)
System.out.println(Math.round(12.67)); // 13 (long)
System.out.println(Math.round(-12.34)); // -12 (long)
System.out.println(Math.round(-12.67)); // -13 (long)
int floatRounded = Math.round(7.8f); // float overload returns int (8)
System.out.println(floatRounded); // 8Terminal Output:
12
13
-12
-13
8
Because Math.round(x) adds +0.5 and takes the floor, ties at .5 always round toward positive infinity (rightward on the number line), so -2.5 rounds to -2 rather than -3:
System.out.println(Math.round(2.5)); // 3 (floor(2.5 + 0.5))
System.out.println(Math.round(-2.5)); // -2 (NOT -3! floor(-2.5 + 0.5))
System.out.println(Math.round(-2.51)); // -3Terminal Output:
3
-2
-3
Passing NaN returns 0, while infinite or out-of-range doubles clamp to Long.MIN_VALUE or Long.MAX_VALUE:
System.out.println(Math.round(Double.NaN)); // 0
System.out.println(Math.round(Double.POSITIVE_INFINITY)); // 9223372036854775807
System.out.println(Math.round(Double.NEGATIVE_INFINITY)); // -9223372036854775808Terminal Output:
0
9223372036854775807
-9223372036854775808
Comparing Math.round(), Math.rint() (Banker's round-half-even), Math.floor(), and Math.ceil():
| Input | Math.round() (long) | Math.rint() (double) | Math.floor() | Math.ceil() |
|---|---|---|---|---|
2.5 |
3 (Toward +∞) | 2.0 (Half-to-even) |
2.0 |
3.0 |
3.5 |
4 (Toward +∞) | 4.0 (Half-to-even) |
3.0 |
4.0 |
-2.5 |
-2 (Toward +∞) | -2.0 (Half-to-even) |
-3.0 |
-2.0 |
-2.6 |
-3 | -3.0 |
-3.0 |
-2.0 |
Rounding a double to 2 decimal places using the 100.0 scaling trick or BigDecimal for financial ledgers:
import java.math.BigDecimal;
import java.math.RoundingMode;
public class DecimalRounder {
public static double round2(double val) {
return Math.round(val * 100.0) / 100.0;
}
public static void main(String[] args) {
System.out.println(round2(19.876)); // 19.88
System.out.println(BigDecimal.valueOf(19.875).setScale(2, RoundingMode.HALF_UP)); // 19.88
}
}Terminal Output:
19.88
19.88
Computing whole-number completion percentages from task counts:
public class ProgressCalculator {
public static int getCompletionPercent(long completed, long total) {
if (total <= 0) return 0;
return (int) Math.round((completed * 100.0) / total);
}
public static void main(String[] args) {
System.out.println("1 of 3 tasks: " + getCompletionPercent(1, 3) + "%"); // 33%
System.out.println("2 of 3 tasks: " + getCompletionPercent(2, 3) + "%"); // 67%
}
}Terminal Output:
1 of 3 tasks: 33%
2 of 3 tasks: 67%
Because Math.round(double) returns a long, dividing by integer 100 performs integer division and truncates your decimals! Always divide by floating-point 100.0:
double raw = 19.876;
double wrong = Math.round(raw * 100.0) / 100; // 1988 / 100 = 19.0 (TRUNCATED!)
double right = Math.round(raw * 100.0) / 100.0; // 1988 / 100.0 = 19.88
System.out.println("By 100: " + wrong + " | By 100.0: " + right);Terminal Output:
By 100: 19.0 | By 100.0: 19.88
Passing a double invokes Math.round(double), which returns a 64-bit long. Assigning it to an int requires an explicit cast: int n = (int) Math.round(doubleVal);.
Writing Math.round(Math.random() * 10) causes severe statistical bias at 0 and 10 (each gets only a 0.5 span instead of 1.0). See Math.random() for the uniform (int) formula.
Compile and run this verification class in any Java environment:
public class MathRoundFullDemo {
public static void main(String[] args) {
// 1. Verify positive and negative .5 tie-breaking
if (Math.round(2.5) == 3L && Math.round(-2.5) == -2L) {
System.out.println("Check 1 Passed: Tie-breaking toward +Infinity verified.");
}
// 2. Verify NaN returns 0
if (Math.round(Double.NaN) == 0L && Math.round(Float.NaN) == 0) {
System.out.println("Check 2 Passed: NaN safely evaluates to 0.");
}
// 3. Verify 2-decimal rounding trick
if (Math.round(3.14159 * 100.0) / 100.0 == 3.14) {
System.out.println("Check 3 Passed: Two-decimal scaling verified.");
}
System.out.println("All Math.round() verification checks passed successfully!");
}
}Terminal Output:
Check 1 Passed: Tie-breaking toward +Infinity verified.
Check 2 Passed: NaN safely evaluates to 0.
Check 3 Passed: Two-decimal scaling verified.
All Math.round() verification checks passed successfully!
Java defines Math.round(x) as floor(x + 0.5), which rounds ties toward positive infinity (-2.5 + 0.5 = -2.0). If you need symmetrical rounding away from zero (-2.5 -> -3), use BigDecimal with RoundingMode.HALF_UP.
Because long and int primitives have no representation for NaN, the Java Language Specification mandates returning 0 (or 0L).
For display, use String.format("%.2f", val). For exact financial math, use BigDecimal.valueOf(val).setScale(n, RoundingMode.HALF_UP).
Math.round() returns an integer primitive (int/long) and breaks .5 ties toward +Infinity. Math.rint() returns a double and breaks .5 ties toward the nearest even integer.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.