# help: 2 digit number problem

Discussion in 'Perl Misc' started by cb, Apr 18, 2004.

1. ### cbGuest

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

cb, Apr 18, 2004

2. ### Tad McClellanGuest

cb <> 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?

--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas

Tad McClellan, Apr 18, 2004

3. ### David EfflandtGuest

On Sun, 18 Apr 2004 15:21:33 +0100, cb <> 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/

David Efflandt, Apr 18, 2004
4. ### cbGuest

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 -----
From: "Tad McClellan" <>
Newsgroups: comp.lang.perl.misc
Sent: Sunday, April 18, 2004 4:34 PM
Subject: Re: help: 2 digit number problem

> cb <> 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?
>
>
> --
> Tad McClellan SGML consulting
> Perl programming
> Fort Worth, Texas

cb, Apr 18, 2004
5. ### Tad McClellanGuest

[ Please do not top-post.
Please do not quote an entire article.
Please provide a conventional attribution when quoting someone.
Thank you.
]

cb <> wrote:
> ----- Original Message -----
> From: "Tad McClellan" <>
> Newsgroups: comp.lang.perl.misc
> Sent: Sunday, April 18, 2004 4:34 PM
> Subject: Re: help: 2 digit number problem
>
>
>> cb <> 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)

--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas

Tad McClellan, Apr 18, 2004
6. ### Joe SmithGuest

Tad McClellan wrote:

> cb <> 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

Joe Smith, Apr 18, 2004
7. ### Guest

Tad McClellan <> wrote:
> >> cb <> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service \$9.95/Month 30GB

, Apr 19, 2004
8. ### Matt GarrishGuest

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

Matt Garrish, Apr 19, 2004

### Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.