How to convert a double to long without casting?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Java, converting a double to a long "without casting" usually means using an API that returns a long for you instead of writing (long) explicitly. The important part is not avoiding the cast syntax itself, but choosing the correct conversion rule because truncation, rounding, overflow handling, and invalid values such as NaN are all different problems.
Decide What Conversion You Actually Want
A double can contain a fractional part, while a long cannot. That means every conversion must answer one question first: what should happen to the fraction?
Common choices are:
- truncate toward zero
- round to the nearest integer
- reject non-integer values
- clamp or reject out-of-range values
There is no one universally correct conversion. The method depends on the semantics your application needs.
Rounding with Math.round
If you want the nearest whole number and you do not want explicit cast syntax, Math.round is the simplest answer. For a double, it returns a long.
This prints 123456790. That is often the best solution for measurements, averages, and display-oriented numeric values where standard rounding is acceptable.
Be careful, though: Math.round is not the same as truncation. If you actually want to drop the fractional part, choose a different method.
Truncation Without Writing a Cast
Java does not have a built-in Double.toLongExact method that truncates automatically without syntax. One clean option is to use BigDecimal or Math.floor and then convert through a method that returns a long.
This prints 123456789. BigDecimal.valueOf is preferable to new BigDecimal(double) because it avoids surprising binary floating-point artifacts in many cases.
Use this approach only if truncation is the intended rule. It does not round.
Rejecting Values That Are Not Exact Integers
Sometimes you do not want rounding or truncation at all. You want the conversion to fail unless the double already represents an exact whole number within long range.
If value were 42.5, this would throw an exception instead of silently changing the number. That is often the right behavior for IDs, counters, or imported numeric fields that must already be integral.
Handling NaN, Infinity, and Range Problems
Before converting, validate special values and numeric range if the data is not fully trusted.
Without checks, special values can produce behavior that is technically defined by the API but not meaningful for your business logic. Validating earlier makes the code's intent much clearer.
Pick the Method by Semantics
A useful rule of thumb is:
- use
Math.roundwhen normal rounding is correct - use
BigDecimal.valueOf(value).longValue()when truncation is acceptable and you want an API call rather than cast syntax - use
longValueExact()with strict rounding rules when bad input should fail fast
This framing is more helpful than treating the problem as "how do I avoid writing parentheses."
Common Pitfalls
The biggest mistake is treating all conversions as interchangeable. Math.round changes the value by rounding, while truncation simply drops the fractional part. Another common issue is using new BigDecimal(double), which can expose floating-point representation noise and make exact comparisons harder to reason about. Developers also forget about NaN, infinity, and overflow, especially when the value came from parsing, external services, or numeric computation rather than a hard-coded constant.
Summary
- Converting
doubletolongwithout explicit cast syntax still requires you to choose a conversion rule. - Use
Math.roundwhen you want alongrounded to the nearest integer. - Use
BigDecimal.valueOf(value).longValue()when truncation is acceptable. - Use exact conversion methods when non-integer input should be rejected.
- Validate special values and range before conversion when input is not fully trusted.

