Use of IsNumeric function in VB.NET

IsNumeric() function returns a value indicating whether an expression can be converted to a numeric data type.
  • 9056

IsNumeric() function returns True if the data type of Expression is BooleanByteDecimal, etc or an Object that contains one of those numeric types, It returns a value indicating whether an expression can be converted to a numeric data type. It also returns True if Expression is a Char or String that can be successfully converted to a number.

IsNumeric returns False if Expression is of data type Date or of data type Object and it does not contain a numeric type, It has to do with the format/type of the data, not the actual value of the data.  And "NaN"  I believe is technically of type float. IsNumeric returns False if Expression is aChar or String that cannot be converted to a number.

Example:

Public
 Class Sample
    Public Shared Sub
 Main()
        Dim Data1 As String = Nothing
        Dim Output As New System.Text.StringBuilder
 
        Output.AppendLine(String.Format( _
           "IsNumeric({0}) ... {1}", Data1, IsNumeric(Data1)))
        Output.AppendLine()
 
        Data1 = "-11.1231"
        Output.AppendLine(String.Format( _
           "IsNumeric({0}) ... {1}", Data1, IsNumeric(Data1)))
        Output.AppendLine()
 
        Data1 = "October 21, 1987"
        Output.Append(String.Format( _
           "IsNumeric({0}) ... {1}", Data1, IsNothing(Data1)))
 
        Console.WriteLine(Output.ToString())
        Console.ReadLine()

    
End Sub
End Class

Output:

number.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.