Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > help: 2 digit number problem

Reply
Thread Tools

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


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-18-2004
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
 
Reply With Quote
 
 
 
 
David Efflandt
Guest
Posts: n/a
 
      04-18-2004
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/
 
Reply With Quote
 
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 -----
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



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-18-2004

[ 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
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      04-18-2004
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
 
Reply With Quote
 
ctcgag@hotmail.com
Guest
Posts: n/a
 
      04-19-2004
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
 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      04-19-2004

"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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
find digit length or the number of numbers in number ? Steven C Programming 8 02-03-2006 04:21 PM
[Newbie]: How to find a specified digit in a number? Mathias Python 3 11-10-2004 06:20 PM
Fastest way to loop through each digit in a number? Rune Strand Python 9 09-06-2004 04:02 PM
match three digit number using regular expression championsleeper Perl 6 04-06-2004 08:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57