1
    double total = item.price * item.quantity;

    if (item.isTaxed == 1) {
        total *= TAX + 1;
    }

    return total;

Consider this; item.price equals 10.99 and item.quantity = 50 and isTaxed evaluates to true.

I want my output to be 620.94 as the total but instead it's giving me 620.93

How do I fix this?

I tried multiplying total by 100 and storing it in an integer and casting back to a double it gives me 620.94 but I get other problems with a validator I'm working with.

mincedMinx
  • 159
  • 1
  • 3
  • 12
  • 1
    You fix it by not using floating point types for fixed point values. – Ignacio Vazquez-Abrams Jul 14 '17 at 01:47
  • Sorry, could you explain or have an example? I'm still having trouble – mincedMinx Jul 14 '17 at 02:18
  • 1
    "but I get other problems ..." -- why don't you fix those other problems? Using an integer to represent a fixed point type is a natural way to go when dealing with currency. See this: https://stackoverflow.com/q/32212213/4996248 – John Coleman Jul 14 '17 at 03:01
  • What is the procedure you use to get the output, `620.93` is likely not the internal value. What is the rounding mode used? What do you get if you print the value with more digits? – Lutz Lehmann Jul 14 '17 at 07:36

0 Answers0