-2

Why is sales tax not adding to the subtotal and total?

    //Value from xQuantity
    double quantity = Convert.ToDouble(xQuantity.Text);

    //Value from xUnitPrice
    double unitPrice = Convert.ToDouble(xUnitPrice.Text);

    //Value from xSubTotal and xTotalPrice
    double SubTotal = quantity * unitPrice;
    xSubTotalTextBox.Text = Convert.ToString(subTotal);

    double Tax = subTotal * 1.06 + subTotal;
    xSubTotalTextBox.Text = Convert.ToString(subTotal);
    xTotalPrice.Text = Convert.ToString(totalPrice);

    //Calculate subtotal and totalprice
    subTotal = Convert.ToDouble(xQuantity.Text) * Convert.ToDouble(xUnitPrice.Text);
    xSubTotalTextBox.Text = subTotal.ToString();
    xTotalPrice.Text = (subTotal * Tax).ToString();         
}

private void xBalance_Click(object sender, EventArgs e)
{
    xRetTextBox.Visible = true;
    xReturn.Visible = true;

    double totalPrice = Convert.ToDouble(xTotalPrice.Text);
    double receive = Convert.ToDouble(xRecvTextBox.Text);
    double subTotal = totalPrice - receive;
    xRetTextBox.Text = Convert.ToString(totalPrice);

    if (totalPrice < .01) xRetTextBox.BackColor = Color.Green;
    else xRetTextBox.BackColor = Color.Red;
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
user1010077
  • 1
  • 1
  • 3
  • [SSCCE](http://sscce.org), please. – Matt Ball Oct 25 '11 at 03:40
  • 1
    I haven't checked the code itself for correctness, but please, please do not use `double` for monetary stuff. Use `decimal`, which doesn't suffer from rounding issues that float and double suffer from. See this question about decimal vs double: http://stackoverflow.com/questions/316727 – Michael Stum Oct 25 '11 at 03:43

2 Answers2

3

Because this line:

 double Tax = subTotal * 1.06 + subTotal;

Should be wrong. Tax cannot be subTotal*1.06+subTotal;

Maybe you meant:

double totalPrice = subTotal * (1.06/100) + subTotal ;
xSubTotalTextBox.Text = Convert.ToString(subTotal);
xTotalPrice.Text = Convert.ToString(totalPrice);
Icarus
  • 63,293
  • 14
  • 100
  • 115
1

The actual tax amount is 0.06*subTotal (assuming 6%). The total (including tax) is 1.06*subTotal.

user973572
  • 169
  • 3