如何在Visual Basic中从String转换为Decimal?

时间:2018-04-24 14:25:04

标签: vb.net winforms

表单有2个列表框,一个用于输入衬衫尺寸和价格,另一个用于显示原始价格,点击按钮后,第二个列表框将原始价格换成折扣价(仅限价格)超过100)它的10%折扣。这部分代码给了我一个错误“无法从字符串转换为十进制” 对于i As Integer = 0到ListBox2.Items.Count - 1         getdiscountedprice(decprice)         Decimal.TryParse(ListBox2.Items(i),decprice)

enter image description here

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim strShirt As String
    Dim strprice As String


    Dim blnmore As Boolean = True
    Do While blnmore
        strShirt = InputBox("Enter shirt: ")
        strprice = InputBox(" Enter shirt price: ")
        If strShirt = String.Empty Or strprice = String.Empty Then
            blnmore = False
        End If

        ListBox1.Items.Add(strShirt & " " & strprice)
        ListBox2.Items.Add(strprice)
    Loop
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim decprice As Decimal


    For i As Integer = 0 To ListBox2.Items.Count - 1
        getdiscountedprice(decprice)
        Decimal.TryParse(ListBox2.Items(i), decprice)

    Next
End Sub
Private Function getdiscountedprice(ByVal Fstrprice As Integer) As Decimal 'cause decimal will be returned 
    Dim lastprice As Decimal

    If Fstrprice > 100 Then
        lastprice = Fstrprice * 0.9
    End If
    Return (lastprice)

End Function

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Me.Close()
End Sub

2 个答案:

答案 0 :(得分:1)

You need to convert the value from a String to a Decimal. You can use the type conversion function CDec

decprice = CDec(ListBox2.Items(i).ToString)

or the TryParse method for Decimal

Decimal.TryParse(ListBox2.Items(i).ToString, decprice)

You have a few issues with your program that need to be resolved. One problem with your program is in the Do While loop within your Button1_Click Sub. If one of the strings either strShirt or strPrice is empty you are setting blnmore to False so that the loop doesn't get executed again, but you are still adding the string values to the list boxes whether they were empty or not. You should change your code to the strings to your list boxes only when they have values:

If strShirt = String.Empty OrElse strprice = String.Empty Then
    blnmore = False
Else
    ListBox1.Items.Add(strShirt & " " & strprice)
    ListBox2.Items.Add(strprice)
End If

Also your getdiscountedprice function is expecting an Integer and you are passing it a Decimal. The function is returning a Decimal and you are not doing anything with that return value such as saving it or printing it. You should be converting the listbox value to Decimal before calling getdiscountedprice, not after. TryParse returns a Boolean indicating if it was successful, you probably only want to call getdiscounted price when you have a valid Decimal value.

If Decimal.TryParse(ListBox2.Items(i).ToString, decprice) Then
   getdiscountedprice(decprice)
End If

答案 1 :(得分:0)

您将一个类型对象放在param中并转换为十进制。

    Public Shared Function ConvertToDecimal(ByVal p_obj As Object, Optional ByVal p_isForUi As Boolean = False) As Decimal
    Dim dRet As Decimal

    If ((p_obj Is DBNull.Value) OrElse (p_obj Is Nothing)) Then
        dRet = Decimal.MinValue
    Else
        If (p_isForUi) Then
            Dim bParseOk As Boolean = Decimal.TryParse(p_obj.ToString(), dRet)
        Else
            Try
                dRet = CDec(p_obj)
            Catch exc As Exception
                dRet = Decimal.MinValue
            End Try
        End If
    End If

    Return dRet