1

Here the problem is when I add amountbeforetax which is 68.0 with tax amount which is 14.96, it is giving me amountWithTax is equal to 82.96000000000001, but when i add with tax amount which is 14.97 or 14.95, 14.98 it is giving me proper value.

what is the problem here? why it is giving me such result?

      public class Calc {
       public static void main(String[] args) {
        double discountAmount = 0;
        double amountBeforeTax = 0;
        double amountWithTax = 0;
        double taxAmount = 0;

        amountBeforeTax = 68.0;
        System.out.println("amountBeforeTax "+amountBeforeTax);
        taxAmount = 14.96;
        System.out.println("taxAmount "+taxAmount);
        amountBeforeTax-= discountAmount;
        System.out.println("amountBeforeTax after discount "+ amountBeforeTax);
        amountWithTax = amountBeforeTax + taxAmount;
        System.out.println("amountWithTax "+ amountWithTax);
    }
}
  • Time and time again people don't know how floating point arithmetic works in computers... Just search for this and you get a million answers – Neijwiert Mar 08 '18 at 12:05
  • 1
    Please consider using BigDecimal for money type for explanation of your issue see here: https://introcs.cs.princeton.edu/java/91float/ – Maciej Mar 08 '18 at 12:08

0 Answers0