double d0 = Double.parseDouble("53.82233040000000557");
double d1 = Double.valueOf("53.82233040000000557");
output
d0 = 53.822330400000006
d1 = 53.822330400000006
double d0 = Double.parseDouble("53.82233040000000557");
double d1 = Double.valueOf("53.82233040000000557");
output
d0 = 53.822330400000006
d1 = 53.822330400000006
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.
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 in a tutorial on Java: BigDecimal and BigInteger
If you need precision, you have to use a BigDecimal
.
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
.