help: 2 digit number problem

C

cb

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
 
T

Tad McClellan

cb said:
How do I check if $number is a positive number to 2 decimal places?


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



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

David Efflandt

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.
 
C

cb

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

Tad McClellan

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


cb said:
----- Original Message -----
From: "Tad McClellan" <[email protected]>
Newsgroups: comp.lang.perl.misc
Sent: Sunday, April 18, 2004 4:34 PM
Subject: Re: help: 2 digit number problem



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)
 
J

Joe Smith

Tad said:
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
 
M

Matt Garrish

David Efflandt said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top