Is Math.round() always reliable for double-to-int conversion in Java?

How do I convert a double to an int in Java without unintended truncation?

In Java, I know that casting a double to an int truncates the decimal part:

double x = 1.5;  
int y = (int) x; // y = 1  

Using Math.round(x) seems to solve this by rounding correctly:

int y = (int) Math.round(x); // y = 2  

However, since floating-point numbers sometimes have imprecise representations (e.g., 1.9999999998), could there be cases where Math.round(x) still results in an unexpected truncation instead of rounding?

Or is it guaranteed that Math.round(x), which returns a long, will always work correctly when converting a double to int in Java?

I’ve been coding Java for a while, and when it comes to rounding a double to an int, the first thing that comes to mind is Math.round(). It’s simple, effective, and works in most cases.

If you need to convert double to int java, Math.round() is a great default choice because it rounds to the nearest whole number rather than truncating.

:small_blue_diamond: Example:

public class ConvertDoubleToInt {
    public static void main(String[] args) {
        double x = 1.9999999998;
        int y = (int) Math.round(x);  // y = 2
        System.out.println(y);
    }
}

:green_circle: Pros:

:heavy_check_mark: Rounds correctly instead of truncating.

:heavy_check_mark: Handles precision issues well.

:red_circle: Consideration:

:warning: Math.round(x) returns a long, so an explicit cast is needed if working with int.

@charity-majors point is solid, but there’s a catch—floating-point precision errors. If you’re working with financial or high-precision data, Math.round() might not be enough. That’s where BigDecimal shines."*

Instead of relying on floating-point operations, use BigDecimal for more accurate rounding when doing a convert double to int java operation.

:bulb: Think of it this way:

:small_blue_diamond: Math.round() → Quick fix

:small_blue_diamond: BigDecimal → Precision matters

:pushpin: Code Example:

import java.math.BigDecimal;
import java.math.RoundingMode;

public class ConvertDoubleToInt {
    public static void main(String[] args) {
        double x = 1.9999999998;
        int y = new BigDecimal(x).setScale(0, RoundingMode.HALF_UP).intValue();
        System.out.println(y); // y = 2
    }
}

:pushpin: When to use BigDecimal?

:heavy_check_mark: No floating-point precision issues.

:heavy_check_mark: Allows choosing different rounding modes (HALF_UP, HALF_DOWN, etc.).

:red_circle: When to skip?

:warning: If you just need a quick and simple rounding operation, Math.round() is easier.

Both the above solutions above are great, but what if you need something different? Sometimes, you don’t want rounding—you want control. That’s when Math.floor() and Math.ceil() are useful.

Imagine you’re dealing with convert double to int java, but instead of rounding normally, you need to always round down (e.g., currency conversions) or always round up (e.g., setting a minimum value).

:pushpin: Real-world analogy:

  • Math.floor() → Always round down, like a discount rounding to the nearest dollar.
  • Math.ceil() → Always round up, like setting a minimum price.

:small_blue_diamond: Example (Always Round Down):

public class ConvertDoubleToInt {
    public static void main(String[] args) {
        double x = 1.9999999998;
        int y = (int) Math.floor(x);  // y = 1
        System.out.println(y);
    }
}

:small_blue_diamond: Example (Always Round Up):

public class ConvertDoubleToInt {
    public static void main(String[] args) {
        double x = 1.2;
        int y = (int) Math.ceil(x);  // y = 2
        System.out.println(y);
    }
}

:pushpin: When to use Math.floor() or Math.ceil()?

:heavy_check_mark: Use Math.floor() if you want to always round down.

:heavy_check_mark: Use Math.ceil() if you want to always round up.

:warning: Avoid if:

If standard rounding is what you need, Math.round() is still the best choice.