Java Math round() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

In Java, Math.round(a) returns the whole number closest to a. Exact halves round up, toward positive infinity: Math.round(2.5) is 3 and Math.round(-2.5) is -2.

Unlike ceil, floor, and rint, it returns an integer type: a long for a double argument and an int for a float. It is the usual choice for rounding prices for display, as in Math.round(x * 100) / 100.0.

A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.round(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass a double to get a long, or a float to get an int.

Java
long a = Math.round(2.5);                    // 3
long b = Math.round(-2.5);                   // -2
int c = Math.round(2.5f);                    // 3 (float version)
double cents = Math.round(x * 100) / 100.0;  // two decimals
CallWhat it does
Math.round(7.49)Returns the long 7.
Math.round(7.5f)float overload: returns the int 8.
Math.round(price * 100) / 100.0Round to two decimal places (a double).
(int) Math.round(x)Nearest int, if you know the value fits.

Since Java 7, round gives the mathematically correct answer even for values just below .5, such as 0.49999999999999994, which rounds to 0. Very old Java versions computed floor(a + 0.5) and returned 1 there.

Library declarations
Java
// java.lang.Math
public static int round(float a)
public static long round(double a)

Parameters

ParameterDescription
aThe float or double to round. An int or long argument is widened to float, so cast it to double if precision matters.

Return Value

Return ValueDescription
int (for float)The nearest int; ties go toward positive infinity.
long (for double)The nearest long. NaN gives 0; values beyond the range give Long.MAX_VALUE or Long.MIN_VALUE (Integer limits for the float version).

Compare with Similar Methods

All rounding options side by side:

MethodHow it differs
rint(a)Nearest, ties to even (2.5 to 2.0); returns double.
ceil(a)Always up; returns double.
floor(a)Always down; returns double.
String.format("%.2f", x)Rounds for display only and returns a String.

Java Examples

Example 1 — Round a Bill to Two Decimal Places

Three items at 6.65 each produce a long, ugly total. Round it to cents for display.

Java
public class MathRoundCents {
    public static void main(String[] args) {
        double total = 3 * 6.65;
        System.out.println("Raw total: " + total);
        System.out.println("Rounded:   " + Math.round(total * 100) / 100.0);

        double tricky = 1.005;
        System.out.println("1.005 to cents: " + Math.round(tricky * 100) / 100.0);
    }
}

How It Works

  1. Binary noise. 6.65 cannot be stored exactly, so 3 * 6.65 prints as 19.950000000000003.
  2. Scale, round, scale back. total * 100 is about 1995.0000000000002, Math.round gives the long 1995, and dividing by 100.0 (a double) prints 19.95.
  3. Not exact for money. 1.005 is stored as 1.00499999..., so 1.005 * 100 is 100.49999999999999, which rounds to 100 and prints 1.0 instead of 1.01.

Example 2 — Negative Ties and the int vs long Result

See how halves round on both sides of zero, and which type each overload returns.

Java
public class MathRoundTies {
    public static void main(String[] args) {
        long up = Math.round(2.5);
        long negTie = Math.round(-2.5);
        long neg = Math.round(-2.6);
        int fromFloat = Math.round(2.5f);
        System.out.println("round(2.5)  = " + up);
        System.out.println("round(-2.5) = " + negTie);
        System.out.println("round(-2.6) = " + neg);
        System.out.println("round(2.5f) = " + fromFloat);
    }
}

How It Works

  1. Half goes up. 2.5 is a tie and round moves it toward positive infinity, to 3.
  2. Up is toward positive infinity. For -2.5, "up" means -2, not -3. Only values past the halfway point, such as -2.6, go to -3.
  3. Type follows the overload. The double calls are stored in long variables; 2.5f is a float, so that call returns an int.

Example 3 — NaN, Huge Values, and the (int) Cast Trap

What round does with values that have no sensible integer answer.

Java
public class MathRoundLimits {
    public static void main(String[] args) {
        System.out.println("round(NaN)    = " + Math.round(Double.NaN));
        System.out.println("round(1e20)   = " + Math.round(1e20));
        System.out.println("round(1e10f)  = " + Math.round(1e10f));
        System.out.println("(int) round(3e9) = " + (int) Math.round(3e9));
    }
}

How It Works

  1. NaN becomes 0. An integer cannot be "not a number", so round returns 0 instead of throwing.
  2. Saturation, not overflow. 1e20 is bigger than any long, so the result is capped at Long.MAX_VALUE; the float version caps 1e10f at Integer.MAX_VALUE.
  3. The cast still wraps. Math.round(3e9) is the correct long 3000000000, but (int) keeps only the low 32 bits, giving -1294967296.

Frequently Asked Questions

The nearest whole number. For a double argument it returns a long, and for a float argument it returns an int. Exact halves round toward positive infinity.
Use Math.round(x * 100) / 100.0 for display, or String.format("%.2f", x) to get text. For exact money calculations, use BigDecimal with setScale(2, RoundingMode.HALF_UP).
Because round sends exact halves toward positive infinity, and -2 is greater than -3. Values past the halfway point, such as -2.6, still round to -3.
Because Math.round(double) returns a long. Write (int) Math.round(3.7), or pass a float: Math.round(3.7f) returns an int.
It returns 0. Values too large for the result type are capped at Long.MAX_VALUE or Long.MIN_VALUE (Integer limits for the float version).

Next: scalb()

Multiply a float or double by a power of two: d times 2 to the n.

scalb() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful