Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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?

mifflin,
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2021-04-16 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?