Search code examples
vb.netdatagridviewsqldatasourcecomparison-operators

In VB.NET evaluate 0.70 to be equal to .7


I am comparing a value that the user entered into a DataGridView cell - which, coming from an Editing Control will be a string to start with - with a decimal value from the data source (defined as decimal(3,2)).

How can I evaluate a user-entered value of ".7", for example, to be equal to the database value of 0.70?


Solution

  • You can use the CDec function to convert a string value to a decimal. e.g.

    If CDec(".7") = 0.7 Then
        ' This will be true
    End If
    

    If you aren't sure that the value entered by the user will be a valid decimal, then you should use Decimal.TryParse:

    Dim value As Decimal = 0
    If Decimal.TryParse(".7", value) Then
        If value = 0.7 Then
            ' This will be true
        End If
    End If