-1

The total value calculated on my form is wrong. 10800 - 10417 * 0.20 = 76.6 is the correct value, but the response in my form is 8716.6 ? What am I doing wrong here ?

My form total is 8716.60, while the correct value as shown on my calculator is 76.6.

    public void wwtax()
    {
        double tax = 0;          
        double value = 0;

        if (tax < 10417)
        {
            tax = Convert.ToInt32(label21.Text);//the 10800 value
            label20.Text = "0";
        }
        if (tax > 10417)
        {
            tax = Convert.ToInt32(label21.Text);
            value = tax - 10417 * 0.20;
        }
        if (tax >= 16777)
        {
            tax = Convert.ToDouble(label21.Text);
            value = tax - 16777 * 0.25 + 1250.00;
        }
        if (tax >= 33333)
        {
            tax = Convert.ToDouble(label21.Text);
            value = tax - 33333 * 0.30 + 5416.67;
        }
        if (tax >= 83333)
        {
            tax = Convert.ToDouble(label21.Text);
            value = tax - 33333 * 0.32 + 201416.67;
        }
        if (tax > 153846)
        {
            tax = Convert.ToDouble(label21.Text);
            value = tax - 153846 * 0.35 + 1001416.67;
        }
        label20.Text = value.ToString();
    }
nacho
  • 5,280
  • 2
  • 25
  • 34
newb
  • 25
  • 4
  • Possible duplicate of [double minus double giving precision problems](https://stackoverflow.com/questions/2741903/double-minus-double-giving-precision-problems) – xdtTransform Feb 06 '19 at 09:21
  • Multiplication is done before subtraction, just adding some brackets should solve your "problem" `(10800 - 10417) * 0.20` – oerkelens Feb 06 '19 at 09:22
  • what datatype can i perform sir ? – newb Feb 06 '19 at 09:22
  • https://learn.microsoft.com/en-us/dotnet/api/system.decimal?redirectedfrom=MSDN&view=netframework-4.7.2#remarks – xdtTransform Feb 06 '19 at 09:23
  • 3
    The response in your form is correct - `10800 - 10417 * 0.20 = 76.6` is wrong. – Tobias Brösamle Feb 06 '19 at 09:23
  • Is it not because a calculator usually completes the evaluation of the last operation before starting the next. Unless you use parenthesis, a calculator will typically calculate `10800 - 10417 = 383` and then `383 * 0.2 = 76.6`, whereas C# will apply PEMDAS (order of operations) and calculate `10417 * 0.2 = 2083.4` first, followed by `10800 - 2083.4 = 8716.6`? – ProgrammingLlama Feb 06 '19 at 09:23
  • so i get parenthesis in the first computation sir ? – newb Feb 06 '19 at 09:25
  • 2
    @xdtTransform quite a bit inaccuracy 76.6 vs. 8716.6, isn't it? :D You won't solve this with using decimals ... ;-) – Nicolas Feb 06 '19 at 09:26
  • 2
    You need to understand the difference (10800 - 10417) * 0.20 = 76.60 and 10800 - 10417 * 0.20 = 8716.60 – PSK Feb 06 '19 at 09:26
  • @Nicolas, I didn't read the question I saw "double", "tax", "`-`", and "wrong value". And made the quick match. As order of operation was to basic to be a possibility. Still using the right type is importent. Sadly I have no dupe target for Order of operation. – xdtTransform Feb 06 '19 at 09:31
  • @newb Take a look as [PEMDAS](https://www.mathsisfun.com/operation-order-pemdas.html) – ProgrammingLlama Feb 06 '19 at 09:31
  • @John, this one is hard it has exponent! – xdtTransform Feb 06 '19 at 09:32
  • 1
    A good calculator would say 8716.6. – Jon Hanna Feb 06 '19 at 09:59

2 Answers2

3

The calculator, which you're using, is probably using just the left to right rule, where the expression results are based on whichever operator is encountered first starting from the left.

But in reality, 10800 - 10417 * 0.20 = 8716.60 is right and 10800 - 10417 * 0.20 = 76.60 is wrong. because unless explicitly specified, multiplication is done before subtraction. The reason is pretty simple. The BODMAS rule. Just search it on the web or refer to some elementary school book.

If you still NEED the answer to be what your calculator is giving you, just use brackets for the part of the expression which you need to calculate first. In your case 10800 - 10417.

Hence your expression should be: value = ( tax - 10417 ) * 0.20;

Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
2

It seems, you should change the order of if (starting from tax > 153846) and use else if. The immediate reason of miscalculation is ommited parenthesis (tax - 10417) * 0.20m)

    // When working with money, Decimal is a better choice than Double
    decimal value = 0;

    // Let's pull out tax from all the if's
    decimal tax = Convert.ToDecimal(label21.Text);

    if (tax > 153846)
      value = (tax - 153846) * 0.35m + 1001416.67m;
    else if (tax >= 83333)
      value = (tax - 33333) * 0.32m + 201416.67m;
    else if (tax >= 33333)
      value = (tax - 33333) * 0.30m + 5416.67m;
    else if (tax >= 16777)
      value = (tax - 16777) * 0.25m + 1250.00m;
    else if (tax > 10417)
      value = (tax - 10417) * 0.20m;
    else 
      value = 0;  

    // "f2": let's ensure 2 digits after the decimal point (i.e. "100.00" for 100)
    label20.Text = value.ToString("f2");
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215