# I'm Sure There's A Better Way

Neil Macneale mac4-devnull at theory.org
Sat Jul 7 04:40:45 CEST 2001

```Note: this sollution requires 2 characters after the decimal point....

def isDollarAmount(s):
try:
if s.count == 1 and s[-3] == '.':
dol, cen = s.split('.')
int(dol)
int(cen)
return 1
int(s)
return 1
except:
return None

The idea is that if the int function can't convert to a number, then the
string is no good.

Hope that helps,  Neil Macneale

In article <3B465858.CB7F38E8 at tundraware.com>, "Tim Daneliuk"
<tundra at tundraware.com> wrote:

> I want a function to check a string to make sure it is a legitimate
> dollar amount.  Which means it follows these rules:
>
> First character is numeric or "-"
> At most, one "." is allowed and, if present, is followed by exactly two
> digits All the remaining characters must be in the range "0" - "9"
>
>
> I wrote the attached to check a string for these rules, but I can't help
> wondering if my feeble newbie python coding isn't already better done
> elswhere and/or using existing logic.  Any ideas all?  TIA.... Code
> follows -
>
>
>
> # Check and see if passed string is a legit amount
>
> def IsNum(num):
>     if num == "":
>         return FALSE
>     digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."] z =
>     num.split(".")
>     if len(z) > 2:             # At Most, only once decimal permitted.
>         return FALSE
>     if len(z) == 2:
>         if len(z[1]) != 2: # Exactly two digits must follow decimal
>             return FALSE
>     if (num[0] == '-') and (len(num) > 1): # 1st char can be sign if
>     more chars follow
>         num = num[1:]    # Drop sign for purposes of checking
>     for x in num:        # Make sure all chars are legit digits
>         if not digits.count(x):
>             return FALSE
>     return TRUE
>

--

--
To reply to me via email, remove the '-devnull' from my address.

```