0
double d0 = Double.parseDouble("53.82233040000000557");
double d1 = Double.valueOf("53.82233040000000557");

output

d0 = 53.822330400000006
d1 = 53.822330400000006
Marvin
  • 12,215
  • 3
  • 50
  • 50
Sam
  • 1
  • 1
  • @hc_dev thanks for the heads up I had misread the title (biased reading, as I'm in the middle of typing up some Js) – David Torrey Nov 27 '19 at 22:27

3 Answers3

4

Precision Numbers in Java

Class java.math.BigDecimal is better for handling numbers where precision matters (like monetary amounts), see BigDecimal VS double.

It could be used for geo-coordinates (latitude/longitude). Although practitionars argue that double is precise enough for lat./long. - since you don't want to locate something at nano-meter scale.

Example Code

If you need high precision and scale for your number, use BigDecimal like this:

BigDecimal decimalValue = new BigDecimal("53.82233040000000557");
System.out.println("as BigDecimal: " + decimalValue.toPlainString());
// prints exactly: 53.82233040000000557

Run this code online (IDE one): Double VS BigDecimal for high precision

Read more

Read more in a tutorial on Java: BigDecimal and BigInteger

hc_dev
  • 5,553
  • 20
  • 27
2

If you need precision, you have to use a BigDecimal.

Michael
  • 924
  • 4
  • 8
1

The answer is that you cannot. The values you are getting are the most accurate approximation to your values that can possibly be stored in a double. There is no possible way to get a more accurate, less rounded value stored in a double.

If you require that the answers are not rounded at all, therefore, you should not be using double. The data type you should be using instead is BigDecimal.

Louis Wasserman
  • 182,351
  • 25
  • 326
  • 397