FAQ 4.71 How do I determine whether a scalar is a number/whole/integer/float?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.


4.71: How do I determine whether a scalar is a number/whole/integer/float?

    Assuming that you don't care about IEEE notations like "NaN" or
    "Infinity", you probably just want to use a regular expression.

       if (/\D/)            { print "has nondigits\n" }
       if (/^\d+$/)         { print "is a whole number\n" }
       if (/^-?\d+$/)       { print "is an integer\n" }
       if (/^[+-]?\d+$/)    { print "is a +/- integer\n" }
       if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
       if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal number\n" }
       if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
                            { print "a C float\n" }

    There are also some commonly used modules for the task. Scalar::Util
    (distributed with 5.8) provides access to perl's internal function
    "looks_like_number" for determining whether a variable looks like a
    number. Data::Types exports functions that validate data types using
    both the above and other regular expressions. Thirdly, there is
    "Regexp::Common" which has regular expressions to match various types of
    numbers. Those three modules are available from the CPAN.

    If you're on a POSIX system, Perl supports the "POSIX::strtod" function.
    Its semantics are somewhat cumbersome, so here's a "getnum" wrapper
    function for more convenient access. This function takes a string and
    returns the number it found, or "undef" for input that isn't a C float.
    The "is_numeric" function is a front end to "getnum" if you just want to
    say, ``Is this a float?''

        sub getnum {
            use POSIX qw(strtod);
            my $str = shift;
            $str =~ s/^\s+//;
            $str =~ s/\s+$//;
            $! = 0;
            my($num, $unparsed) = strtod($str);
            if (($str eq '') || ($unparsed != 0) || $!) {
                return undef;
            } else {
                return $num;

        sub is_numeric { defined getnum($_[0]) }

    Or you could check out the String::Scanf module on the CPAN instead. The
    POSIX module (part of the standard Perl distribution) provides the
    "strtod" and "strtol" for converting strings to double and longs,


Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.


    Copyright (c) 1997-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. All rights

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

Site Timeline