For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > April 2004 > Re: number problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: number problem
Matt Garrish

2004-04-18, 2:30 pm


"cb" <cb2005uk@yahoo.co.uk> wrote in message
news:40829739$0$15953$cc9e4d1f@news.dial.pipex.com...
> I'm sure this is very simple, tried searching google for 2 hrs.
>
> Problem
> =======
> How do I check if $number is a positive number to 2 decimal places?
>
> ie.
>
> 12.12 valid
> 0 invalid
> 0.00 invalid
> 1.0 invalid
> 12.123 invalid
> -1.00 invalid
>


The following should do what you want:

foreach my $number (12.12, 0, 0.00, 1.0, 12.123, -1.00) {

if ( ($number > 0) && ($number =~ /^\d+\.\d{2}$/) ) {

print $number;

}

}


Matt


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com