我正在为我的视觉基础课程编写一个程序来计算每磅罐头的价格。我有程序运行,但我需要将美分四舍五入到最接近的美元。我怎么能在vb中这样做?是否可以舍入一个十进制变量?
我很抱歉,如果这听起来很愚蠢,我是个新手。
这是我的代码。
Public Class Form1
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'Variables
Dim cans As Integer
Dim canPounds As Integer
Dim canPoundTotal As Decimal
Dim canWorth As Decimal = 0.75
Dim cansNeeded As Integer
'Conversions
cans = Convert.ToInt32(txtGoal.Text)
'Calculation
canPounds = cans / 24
canPoundTotal = canPounds * canWorth
cansNeeded = 33000 - cans
If cboNeed.SelectedIndex = 0 Then
lblOutput.Text = cansNeeded.ToString + " cans need to be collected to reach your goal"
ElseIf cboNeed.SelectedIndex = 1 Then
lblOutput.Text = canPoundTotal.ToString("C") & " will be earned by collecting cans for recycling"
End If
End Sub
Private Sub cboNeed_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboNeed.SelectedIndexChanged
If cboNeed.SelectedIndex = 0 Then
lblDesc.Text = "Target Goal Amount"
btnCalculate.Text = "Find Target Amount of Cans"
ElseIf cboNeed.SelectedIndex = 1 Then
lblDesc.Text = "Cans Collected"
btnCalculate.Text = "Find Amount Earned"
End If
End Sub
End Class
答案 0 :(得分:1)
您可以使用以下函数:Math.Round(value)