Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Checking for numbers and a period

by krusty (Hermit)
on Jul 21, 2002 at 03:02 UTC ( #183735=note: print w/ replies, xml ) Need Help??


in reply to Checking for numbers and a period

So the possibilities are that a price might fall into the following catagories:

1) 300.25
2) 300.
3) 300
4) .25

or if you want to visit Europe, replace the periods with commas.

my $price = $some_price_formatted_like_above; if ($price =~ /\d+[.,]?\d*|[.,]\d+/) {...}
...where the first portion of the pattern matches the first three possible formats listed above and the second portion matches a format like .25.
Have I missed any formats?

Cheers,
Kristina
UpdatePlease include the anchors ^$ as in Corion's update. It will prevent the if statement from running as my code would return true for 300.25.25.
Corion's code misses fractions of cents. Like if something is 99 and 1/2 cent. This may be a desirable trait though. My code misses the European spaces. All the code I've seen so far also misses things like 1,000,000,000.25. This can still be written for, but how far do you really need to go to accomplish your task?


Comment on Re: Checking for numbers and a period
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2014-07-31 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (250 votes), past polls