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?
a source to share
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
a source to share
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.
a source to share
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
a source to share