Velocity Reviews > Perl > help: 2 digit number problem

# help: 2 digit number problem

cb
Guest
Posts: n/a

 04-18-2004
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

thanks

Guest
Posts: n/a

 04-18-2004
cb <(E-Mail Removed)> wrote:

> How do I check if \$number is a positive number to 2 decimal places?

print "valid\n" if \$number =~ /^\d*\.\d\d\$/; # untested

> ie.

You meant "eg." rather than "ie." there.

> 0.00 invalid

Why is that invalid?

Looks like 2 decimal places to me...

Is it a mistake in your examples or a mistake in your specification?

--
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas

David Efflandt
Guest
Posts: n/a

 04-18-2004
On Sun, 18 Apr 2004 15:21:33 +0100, cb <(E-Mail Removed)> wrote:
> 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?

This works as a test:

my \$x = shift || die "enter number on command line\n";
\$x += 0; # to assure a number instead of string
if (\$x > 0 && \$x =~ /\.\d\d\$/) {
print "\$x valid\n";
} else {
print "\$x invalid\n";
}

Note that the '\$x += 0;' assures that any trailing decimal zeros used in
string context are dropped.

--
David Efflandt - All spam ignored http://www.de-srv.com/

cb
Guest
Posts: n/a

 04-18-2004
No, I didn't want 0.00 to be valid.
I want a user to enter a start price for an auction, I don't want 0.00 or
negative numbers to be valid.

if \$number =~ /^\d*\.\d\d\$/;
works fine

I've added a second check to look for 0.00

thanks

----- Original Message -----
Newsgroups: comp.lang.perl.misc
Sent: Sunday, April 18, 2004 4:34 PM
Subject: Re: help: 2 digit number problem

> cb <(E-Mail Removed)> wrote:
>
> > How do I check if \$number is a positive number to 2 decimal places?

>
>
> print "valid\n" if \$number =~ /^\d*\.\d\d\$/; # untested
>
>
> > ie.

>
>
> You meant "eg." rather than "ie." there.
>
>
> > 0.00 invalid

>
>
> Why is that invalid?
>
> Looks like 2 decimal places to me...
>
> Is it a mistake in your examples or a mistake in your specification?
>
>
> --
> (E-Mail Removed) Perl programming
> Fort Worth, Texas

Guest
Posts: n/a

 04-18-2004

Please do not quote an entire article.
Thank you.
]

cb <(E-Mail Removed)> wrote:
> ----- Original Message -----
> From: "Tad McClellan" <(E-Mail Removed)>
> Newsgroups: comp.lang.perl.misc
> Sent: Sunday, April 18, 2004 4:34 PM
> Subject: Re: help: 2 digit number problem
>
>
>> cb <(E-Mail Removed)> wrote:
>>
>> > How do I check if \$number is a positive number to 2 decimal places?

>> > 0.00 invalid

>>
>> Why is that invalid?
>>
>> Looks like 2 decimal places to me...
>>
>> Is it a mistake in your examples or a mistake in your specification?

> No, I didn't want 0.00 to be valid.

So then, it was a mistake in your specification.

> I've added a second check to look for 0.00

Does your "check" use the == operator? (it should)

--
(E-Mail Removed) Perl programming
Fort Worth, Texas

Joe Smith
Guest
Posts: n/a

 04-18-2004

> cb <(E-Mail Removed)> wrote:
>>How do I check if \$number is a positive number to 2 decimal places?
>>0.00 invalid

>
> Why is that invalid?
>
> Looks like 2 decimal places to me...
>
> Is it a mistake in your examples or a mistake in your specification?

0.00 falls into the category of non-negative numbers, but it is
not a positive number, which was explictly stated in the specification.
-Joe

ctcgag@hotmail.com
Guest
Posts: n/a

 04-19-2004
> >> cb <(E-Mail Removed)> wrote:
> >>
> >> > How do I check if \$number is a positive number to 2 decimal places?

> > No, I didn't want 0.00 to be valid.

>
> So then, it was a mistake in your specification.

No, 0.00 is not positive. Although it is nonnegative.

Xho

--
Usenet Newsgroup Service \$9.95/Month 30GB

Matt Garrish
Guest
Posts: n/a

 04-19-2004

"David Efflandt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sun, 18 Apr 2004 15:21:33 +0100, cb <(E-Mail Removed)> wrote:
> > 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?

>
> This works as a test:
>
> my \$x = shift || die "enter number on command line\n";
> \$x += 0; # to assure a number instead of string
> if (\$x > 0 && \$x =~ /\.\d\d\$/) {
> print "\$x valid\n";
> } else {
> print "\$x invalid\n";
> }
>
>
> Note that the '\$x += 0;' assures that any trailing decimal zeros used in
> string context are dropped.
>

But as I read his question, 0.10, 1.00, 1.10, etc. are all valid, which
won't be the case when you strip the trailing zeros as per your example.

Matt