How can I multiply two double numbers without loss of precision?

Byron Tymvios

To avoid loss of precision you need to wrap the doubles in a BigDecimal object. Then you can use the multiply(BigDecimal) method of BigDecimal to do the calculation. The method accepts a BigDecimal as an argument and returns a new BigDecimal which will contain the result.

double d1 = 12345.67d;
double d2 = 76543.21d;

BigDecimal bd1 = new BigDecimal(d1);
BigDecimal bd2 = new BigDecimal(d2);

BigDecimal result = bd1.multiply(bd2);

System.out.println("The result is: " + result.doubleValue());