is there any is integer function ?

Discussion in 'ASP .Net' started by Tee, Feb 27, 2004.

  1. Tee

    Tee Guest

    Hi,

    how do we detect if a value is a positive integer ?

    thanks,
    Tee
     
    Tee, Feb 27, 2004
    #1
    1. Advertisements

  2. Tee

    TJS Guest

    if myValue > 0 then ...
     
    TJS, Feb 27, 2004
    #2
    1. Advertisements

  3. how do we detect if a value is a positive integer ?

    Are you using VB.NET or C#? In C# I'd do:

    if (x is int)
    {
    if ((int) x) > 0)
    // x is a positive integer
    else
    // x is a negative integer
    }
    else
    // x is NOT an integer at all!


    In VB.NET you can use IsNumeric().

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com
    http://www.ASPFAQs.com
    http://www.ASPMessageboard.com

    * When you think ASP, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Feb 27, 2004
    #3
  4. Tee

    Scott M. Guest

    And how would this know the difference between 1 and 1.1?

    if x > 0 AND x = CTYPE(x, Integer) then
     
    Scott M., Feb 27, 2004
    #4
  5. Tee

    Tee Guest

    Hi,

    Thanks for your reply,
    I know about the isnumeric, but ... whatever number value will return true
    with isnumeric, so I hope to get something like isInteger() ...

    thanks.
    Tee
     
    Tee, Feb 27, 2004
    #5
  6. Tee

    Tee Guest

    Great idea,

    Thanks.


     
    Tee, Feb 27, 2004
    #6
  7. Tee

    Harsh Thakur Guest

    Hi,

    As noted before, for a positive integer, you can just say:
    if(x > 0)
    To test whether x is in integer, you can say:
    x = int.Parse(value.ToString())
    where value is the number you want to check.

    If value represents an integer, x will contain it after
    the step given above. Else an FormatException will be
    thrown.

    For more details you can check out int.Parse in MSDN.

    HTH
    Regards
    Harsh Thakur
     
    Harsh Thakur, Feb 27, 2004
    #7
  8. with isnumeric, so I hope to get something like isInteger() ...

    You need to re-read Scott's reply. That was the first thing he tested for.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.
     
    Kevin Spencer, Feb 27, 2004
    #8
  9. Tee

    JesseChallis

    Joined:
    Jul 1, 2010
    Messages:
    1
    Likes Received:
    0
    A different Approach

    public bool IsNumber(string text)
    {
    bool result = true;
    foreach (var c in text)
    {
    if (!Char.IsDigit(c))
    {
    result = false;
    break;
    }
    }

    return result;
    }

    Cheers
     
    JesseChallis, Jul 1, 2010
    #9
  10. Tee

    rutgers

    Joined:
    Feb 28, 2011
    Messages:
    1
    Likes Received:
    0
    Try this:


    If IsNumeric(UserInputTextBox.Text) And (System.Math.Abs(val(UserInputTextBox.Text) Mod 1) <> 0) Then
    '______Do something
    End if

    ************************
    OR Atfer checking IsNumeric(UserInputTextBox.Text)
    Do this following:

    if (System.Math.Abs(val(UserInputTextBox.Text) Mod 1) <> 0)
     
    Last edited: Feb 28, 2011
    rutgers, Feb 28, 2011
    #10
  11. Tee

    kmoloney

    Joined:
    Jan 27, 2012
    Messages:
    1
    Likes Received:
    0
    Positive Integer Function in vb.net

    In vb.net you get a cast error if you pass a string in some functions. This should take care of it:


    Private Function IsPositiveInteger(ByVal objValue AsObject) As Boolean
    Dim intX AsInteger
    If Not (IsNumeric(objValue)) Then
    Return False
    Else
    intX = CInt(objValue)
    If objValue > 0 Then
    Return True
    Else
    Return False
    End If
    End If
     
    Last edited: Jan 27, 2012
    kmoloney, Jan 27, 2012
    #11
  12. Tee

    AJBiagioli

    Joined:
    Jan 30, 2012
    Messages:
    1
    Likes Received:
    0
    ' inputString is the string we want to test.
    Dim number As Integer
    If TryParse(inputString, number) AndAlso number > 0
    '''' Continue here ....
    End If
     
    AJBiagioli, Jan 30, 2012
    #12
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.