1
double d = 1000000000138719341D;
System.out.print(Double.valueOf(d).longValue());

The printed result is

1000000000138719360

Why is it different? And how do I get the correct result?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

2 Answers2

1

From the docs:

double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
1

you need java.math.BigDecimal class

andy.hu
  • 325
  • 1
  • 2
  • 10