SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to check if a number is integer

    hello

    I have to check if a $num (number) is a integer or not . How to do with perl please ?

    For example the code should return yes if $num=1
    and "no" if $num=1.45


    Thank you
    Graziano

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in your example this would be simple:

    Code:
    if (/\D/) {
       is not an integer
    }
    the regular expression checks if there are any non-digit characters in the string. Since there is a dot it returns true and the string is not an integer. But that might be too simple minded for your real data, in that case see the perl FAQs:

    How do I determine whether a scalar is a number/whole/finteger/float

    There is also a module(s) that the above link mentions you can use.


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
  •