locked
isnumeric function in VB.net RRS feed

  • Question

  • User351619809 posted

    Hi All,

      I am checking in my code if the line is numeric then do this otherwise do something else.

    if IsNumeric(line) = true then

       ' do something

    else

      'do something else

    some of the numbers that have 2. or 3., they are considered to be numeric. Is there any way, I can check that they are not numeric since they are suffixed by a period.

    I tried doing this:

    If IsNumeric(line)= False Or line.Substring(1, 1) = "." Then
    
    do soemthing
    

     

    It does not seem to be that robust solution. Is there any other way, I can check for only numeric values. If there is a period or any other character then it won't be considered as numeric.

    any help will be appreciated.

    Update: I think I got it. I just have to do Integer.TryParse and that will only give integer values.

    Thursday, January 14, 2016 9:13 PM

Answers

  • User-986267747 posted

    Hi anjaliagarwal5,

    some of the numbers that have 2. or 3., they are considered to be numeric. Is there any way, I can check that they are not numeric since they are suffixed by a period.

    IsNumeric Function Returns a Boolean value indicating whether an expression can be evaluated as a number. You could use the following code to meet your requirement.

    Public Class WebForm2
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim testVar As Object
    
            testVar = "53"
    
            testVar = "459.95"
    
            'testVar = "45 Help"
    
    
            If Not IsNumeric(testVar) Or testVar.ToString.Contains(".") Then
                TextBox1.Text = "NotIsNumeric"
            Else
                TextBox1.Text = "IsNumeric"
            End If
    
        End Sub
    
    
    
    End Class

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 15, 2016 6:24 AM

All replies

  • User753101303 posted

    Hi,

    I would suggest Decimal.TryParse for example. I don't know if 2. would pass but at least you have the guarantee that the string can be converted to a numeric value). Note also that the result dépends on which culture is used.

    Thursday, January 14, 2016 9:26 PM
  • User-986267747 posted

    Hi anjaliagarwal5,

    some of the numbers that have 2. or 3., they are considered to be numeric. Is there any way, I can check that they are not numeric since they are suffixed by a period.

    IsNumeric Function Returns a Boolean value indicating whether an expression can be evaluated as a number. You could use the following code to meet your requirement.

    Public Class WebForm2
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim testVar As Object
    
            testVar = "53"
    
            testVar = "459.95"
    
            'testVar = "45 Help"
    
    
            If Not IsNumeric(testVar) Or testVar.ToString.Contains(".") Then
                TextBox1.Text = "NotIsNumeric"
            Else
                TextBox1.Text = "IsNumeric"
            End If
    
        End Sub
    
    
    
    End Class

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 15, 2016 6:24 AM