我的问题是当我按下bigmacadd按钮和减号按钮时,bigmac正常工作。但是当我按下mcdouble添加按钮时,我得到了一堆数字。我如何才能使它显示2.50作为小数位?
Public Class Form1
Const bigmac As Decimal = 4D
Const mcdouble As Decimal = 2.25
Dim tax As Decimal
Dim price As Decimal
Dim quantity As Integer
Dim finaltotal As Decimal
Private Sub Btnbigmacadd_Click(sender As Object, e As EventArgs) Handles Btnbigmacadd.Click
quantity = quantity + 1
txtquan.Text = quantity
price += bigmac
txtprice.Text = price
tax = price * 0.15
txttax.Text = tax
finaltotal = price + tax
txtfinaltotal.Text = finaltotal
End Sub
Private Sub btnbigmacminus_Click(sender As Object, e As EventArgs) Handles btnbigmacminus.Click
quantity = quantity - 1
txtquan.Text = quantity
price -= bigmac
txtprice.Text = price
tax = price * 0.15
txttax.Text = tax
finaltotal = price + tax
txtfinaltotal.Text = finaltotal
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
quantity = quantity + 1
txtquan.Text = quantity
price += mcdouble
txtprice.Text = price
tax = price * 0.15
txttax.Text = tax
finaltotal = price + tax
txtfinaltotal.Text = finaltotal
End Sub
End Class
答案 0 :(得分:2)
您可以使用tax
方法
Math.Round
舍入到两位小数
tax = Math.Round(price * 0.15D, 2)
txttax.Text = tax.ToString()
finaltotal = price + tax
txtfinaltotal.Text = finaltotal.ToString()
如果要在tax
和finaltotal
中保留额外的小数位数,但在文本框中只显示两位小数,则可以使用ToString("N2")
方法。
tax = price * 0.15D
txttax.Text = tax.ToString("N2")
finaltotal = price + tax
txtfinaltotal.Text = finaltotal.ToString("N2")