Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: How to determine if something is numeric?

by Limbic~Region (Chancellor)
on Jan 30, 2004 at 19:28 UTC ( #325348=note: print w/replies, xml ) Need Help??

in reply to How to determine if something is numeric?

You haven't done a very good job of explaining exactly what you mean by numeric. Are you only looking for integers or do decimals count? You say no alphas, but is a larger integer represented in scientific notation acceptable? What about Inf and NaN? I would suggest having a look at Scalar::Util's looks_like_number but that doesn't seem to be what you want.

If you are looking for positive or negative whole integers that do not have any leading zeros, the following should work. Remember 0 by itself will not be numeric.

#!/usr/bin/perl -w use strict; my $test = -413; print "$test is ok\n" if numeric( $test ); sub numeric { my $number = shift; return 0 if ! $number; my $first = substr( $number , 0 , 1 ); $number = substr($number, 1) if $first eq '-'; return 1 if $number =~ /^[1-9]\d+?$/; return 0; }
Cheers - L~R

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://325348]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2021-04-16 10:31 GMT
Find Nodes?
    Voting Booth?

    No recent polls found