I have the following JAVA code where I calculate a customer due amount
Double invsubtot = 0.0;
Double paidtot = 0.0;
Double duetot = 0.0;
try {
ResultSet invoicesubtotal = InfoIMS.DbConnection.searchResult("select invoice_subtotal from invoice where customer_id = '69' and payment_type = 'Credit' and invoice_status = '1' group by invoice_number");
while (invoicesubtotal.next()) {
invsubtot = invsubtot + invoicesubtotal.getDouble(1);
}
// customerduejLabel5.setText(String.valueOf(invsubtot));
ResultSet paidtotal = InfoIMS.DbConnection.searchResult("select sum(payment) from payments where cusid = '69' and ingrtype='1' and payment_status = '0'");
while (paidtotal.next()) {
paidtot = paidtotal.getDouble(1);
}
// customerduejLabel5.setText(String.valueOf(paidtot));
duetot = invsubtot - paidtot;
customerduejLabel5.setText("Customer Due : " + String.valueOf(duetot));
But the Output result is Customer Due : 6999.999999999999
But the real values of the first & second MySql queries are as follows
1. invoice_subtotal = 13891.30
2. sum(payment) = 6891.30
So it should be giving me the output as Customer Due : 7000 But Why is it giving me 6999.999...? Any way to solve this?
And I am not satisfied with the indirect answers given in Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273? Where it shows why it happens but not how to avoid it clearly