爲什麼銷售稅不會增加小計和總額?在收銀員計劃中計算銷售稅
//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;
}
[SSCCE](http://sscce.org),請。 –
我沒有檢查代碼本身的正確性,但請不要使用'double'作爲貨幣。使用'decimal',它不會遇到float和double遭遇的舍入問題。看到這個問題關於十進制與雙精度:http://stackoverflow.com/questions/316727 –