2011-10-25 103 views
-2

爲什麼銷售稅不會增加小計和總額?在收銀員計劃中計算銷售稅

//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; 
} 
+0

[SSCCE](http://sscce.org),請。 –

+1

我沒有檢查代碼本身的正確性,但請不要使用'double'作爲貨幣。使用'decimal',它不會遇到float和double遭遇的舍入問題。看到這個問題關於十進制與雙精度:http://stackoverflow.com/questions/316727 –

回答

3

因爲該行:

double Tax = subTotal * 1.06 + subTotal; 

應該不會錯。稅款不能是subTotal*1.06+subTotal;

也許你的意思是:

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

它沒有工作。 – user1010077

+0

@ user1010077那麼除了我指出的問題之外,你還有更多的問題。 – Icarus

+0

好吧..我把這一行稅改爲1.06,它的工作原理 xTotalPrice.Text =(subTotal * Tax).ToString(); – user1010077

1

實際稅額0.06 *小計(假設6%)。總計(含稅)爲1.06 *小計。