+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 24

Thread: Check if integer or float!

  1. #1
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Check if integer or float!

    hi people
    i need to check if the input thing is integer and another is float.

    how do i that?
    i think that isnumeric will not differentiate between integre and float.
    Will i have to use Regex?

    Thanx
    VB.NET 2005

    micky

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    You could try this:-

    Code:
    Try
      Convert.ToInt16(value)
    catch
      'not an integer
    end try
    

  3. #3
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Thanx RR

    will this fail if the entered value is not an integer??
    also, i was hoping to have something like a Regex so that i can make it in a function to use across th app!!

  4. #4
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by micky
    will this fail if the entered value is not an integer??
    It should throw an exception if it fails which it would if the value wasn't an integer....

    You could put this into a function....

    You could use a regex match instead if you wanted...

  5. #5
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Quote Originally Posted by richyrich View Post
    You could use a regex match instead if you wanted...
    do u have it or can u lead me to a place where i can find it?

    i need it for float too

  6. #6
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Try something like this:-

    Code:
    Dim regex_int as Regex = New Regex("^[0-9]$")
    Dim regex_int_dec as Regex = New Regex("^[0-9]+\.[0-9]{1,2}$")
     
    if regex_int.IsMatch(value) or regex_int_dec.IsMatch(value) then
    'value matches either regex
    else
    'value doesn't match either regex
    end if
    
    <edit>Actually I suppose you could just use Dim regex_int as Regex = New Regex("^[0-9\.]$")</edit>
    Last edited by richyrich; January 9th, 2009 at 07:40 AM.

  7. #7
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    thanx RR
    i'll check and revert back if some problem

  8. #8
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Another way that should work is using TryCast. See here: TryCast
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  9. #9
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    thanx J, but i just needed regex for the two

  10. #10
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    RR, the regex for float rejects integers.
    shud it be like that??

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast

Similar Threads

  1. Check if result set is empty
    By noFriends in forum PHP Development
    Replies: 9
    Last Post: July 8th, 2011, 10:06 AM
  2. Check for existing data in field using SP
    By micky in forum SQL Development
    Replies: 17
    Last Post: November 4th, 2008, 09:09 AM
  3. Dynamically created ddl check if matching selections
    By peebman2000 in forum .NET Development
    Replies: 10
    Last Post: October 30th, 2008, 10:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO