Welcome, guest | Sign In | My Account | Store | Cart

just a simple little program that able to check the data key-in is in form of money or not.

Python, 29 lines
 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
# isMoney.py
'''A simple program to check a data is in money datatype'''

def isMoney():
#   format = "123.45"
    money = 123
    flag = 0
    
    # check - all is digits ?
    if str(money).isdigit() == 1:
        flag = 1 
    else:
        # loop into the data
        for i in range (0, len(str(money)) + 1):
            # check - decimal point exists ?
            if str(money)[i:i+1] == ".":
                # check - all is digits (except the ".") ?
                if str(money)[i+1:].isdigit() == 1 and str(money)[:i].isdigit() == 1:
                    flag = 1

    # check - is money ?
    if flag == 1:
        print "$" + "%.2f" % float(money)
    else:
        print money, "is not money datatype"

    
if __name__ == '__main__':
    isMoney()

pls give your opinion or do some upgrade about the program.. your opinion makes a lot of meaning to me.. thanks

8 comments

Markus Weihs 16 years, 11 months ago  # | flag

Hi!

To just check if the number is in money format and valid money formats are "123" and "123.04" I would do something like this:

def isMoney(money):
    money = str(money)
    if money.isdigit(): return True
    else:
        try:
            new = "%.2f" % float(money)
            return new == money
        except: return False

if __name__ == '__main__':
    print isMoney("123.004")  # False
    print isMoney(123)        # True
    print isMoney("123.04")   # True
    print isMoney("1.2.3")    # False
    print isMoney("hello")    # False
waikeong chan (author) 16 years, 11 months ago  # | flag

Hi Markus! yeah! your code is looks smartter Hi Markus! yeah! your code is looks smartter

waikeong chan (author) 16 years, 11 months ago  # | flag

Hi Markus! yeah! your code is looks smartter Hi Markus! yeah! your code is looks smartter

waikeong chan (author) 16 years, 11 months ago  # | flag

but it cant function when i key in "123.4". i expect it return $123.40 as the answer; but it return false!!

Markus Weihs 16 years, 11 months ago  # | flag

Hi!

My function just checks if the data is in money-format. If you want to convert it to this format, you can do this:

def to_money(data):
    try:
        return "$%.2f" % data
    except:
        return False  # or some error message

print to_money(123.4)   # -> $123.40
waikeong chan (author) 16 years, 11 months ago  # | flag
Hi!
how do you think about the following code?

def isMoney(money):
    money = str(money)
    if money.isdigit():
        return "%.2f" % float(money)
    else:
        try:
            new = "%.2f" % float(money)
            return new
        except: return False

if __name__ == '__main__':
    print isMoney("123.4")    # True
    print isMoney(123)        # True
    print isMoney("123.04")   # True
    print isMoney("1.2.3")    # False
    print isMoney("hello")    # False

it's looks better <pre>
Hi!
how do you think about the following code?

def isMoney(money):
    money = str(money)
    if money.isdigit():
        return "%.2f" % float(money)
    else:
        try:
            new = "%.2f" % float(money)
            return new
        except: return False

if __name__ == '__main__':
    print isMoney("123.4")    # True
    print isMoney(123)        # True
    print isMoney("123.04")   # True
    print isMoney("1.2.3")    # False
    print isMoney("hello")    # False

it's looks better

</pre>

Peter Bengtsson 16 years, 11 months ago  # | flag

What about , comma. In some countries the default delimiter on decimal value is the , comma character. Not the . dot.

bu66le - 16 years, 10 months ago  # | flag

isMoney with re.

def isMoney(str):
import re
re_money = re.compile(r'\$([0-9]{,3}\,?)+(\.[0-9]{,2})?$')
    if(re_money.match(str)):
        print 'true'
    else:
        print 'false'

isMoney('0.2') #false
isMoney('$0.2') #true
isMoney('$0.20') #true
isMoney('10.234') #false
isMoney('$1,100.0.001829') #false
isMoney('$1,000,000.20') #true
isMoney('$1.1,0') #false
Created by waikeong chan on Fri, 31 Dec 2004 (PSF)
Python recipes (4591)
waikeong chan's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks