Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Re: How to check Inputs if Numeric

by kasmot (Novice)
on Apr 01, 2004 at 03:14 UTC ( [id://341493]=note: print w/replies, xml ) Need Help??


in reply to Re: How to check Inputs if Numeric
in thread How to check Inputs if Numeric

Thanks to all. You guys here were really very good and of great help.. More Power..

Replies are listed 'Best First'.
Re^3: How to check Inputs if Numeric
by Anonymous Monk on Mar 06, 2014 at 10:29 UTC

    I know this thread is old but it came up on google firstish. I searched, then came up with my own line, which I think works better than the regexp. I did not check all over, so most likely it is already around somewhere.

    if ( $input+0 eq $input ){ print "it is a number!\n";}

    Can someone point out the problem with this approach? Advantage for me: easy + quick, no problems with signs...

      Depends on whether you'd consider "000" a number, for example. (I would, probably, in most cases.)

      I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

      Not bad, especially given the simplicity. More potential false negatives to go with jdporter's.

      for my $input (qw< +4 1.0 1e5 12345678901234567890 1.234567890123456789 >) { if( $input+0 eq $input ) { print "$input is a number!\n"; } else { print "$input is not ", 0+$input, "\n"; } } __END__ +4 is not 4 1.0 is not 1 1e5 is not 100000 12345678901234567890 is not 1.23456789012346e+019 1.234567890123456789 is not 1.23456789012346

      Update: It successfully detects if $input is (primarily) holding a numeric value (an IV or NV, how Perl stores an integer and floating point, respectively) or is holding a string value that equals the canonical string representation of any possible Perl numeric value.

      So I don't believe that there are any false positives. The false negatives are the many ways to reasonably represent a numeric value that aren't Perl's canonical representation.

      - tye        

      just a quick (final) note : ($nr+0 eq $nr ) is not perfect for checking on numbers in general, but it is a very good check for integer numbers.

      It avoids perl complaining about casting as some other methods would and is pretty easy on resources

      If you don't mind "nan" and "inf" among numbers...
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      thanks for the replies!

      some of the problems pointed out would not bother me much IRL ( i.e. most of my applications ), the one with 1.0 for example would. Back to regexp's I suppose :/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-03-19 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found