IsDouble check string in Vb.net?

I will get the data in DataTable

. I'm going to iterate over data in foreach

. I will have all data types in DataTable

. Now I need to find Double

for each element ( string

) in DataTable

. How do I find IsDouble

for a string?

Example:

I have a string "21342.2121"

. I need to hide this until Double

. But sometimes there will be data "TextString"

. Therefore, I cannot use Double.Parse()

.

How to deal with this?

+2


a source to share


5 answers


This is the wrong approach, you need to know what each column in the data table is. Run this program to see what might go wrong:



Module Module1
    Sub Main()
        Dim value As Double
        If Double.TryParse("1e0", value) Then
            Console.WriteLine("Uh-oh")
        End If
        Console.ReadLine()
    End Sub
End Module

      

+1


a source


Try it Double.TryParse

. This will return false if the number is not in a valid / recognized format, which allows you to do whatever you need to do in this scenario.



+6


a source


Dim val as Double
Double.TryParse("MyString", val)
Double.TryParse("1234.567", val)

      

The first TryParse () will return false. The second TryParse () will return true and place 1234.567 in val

.

+5


a source


Just to expand on the (correct) answers already provided, here's a complete usage example Double.TryParse

:

Dim value As Double
If Double.TryParse(stringFromDataTable, value) Then
    ' text has been parsed as value, '
    ' so you can use value however you see fit '
Else
    ' text was not a valid double, so you can '
    ' notify the user or do whatever you want... '
    ' note that value will be zero in this case '
End If

      

+5


a source


May I ask why your save is doubled as strings and mixed with non-numeric string values? It looks like you would like to avoid this, all costs.

+2


a source







All Articles