ActiveState Code

Recipe 361333: isMoney


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

Python
 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()

Discussion

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

Comments

  1. 1. At 3:30 a.m. on 31 dec 2004, Markus Weihs said:

    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
    
  2. 2. At 5:07 p.m. on 2 jan 2005, waikeong chan (the author) said:

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

  3. 3. At 5:07 p.m. on 2 jan 2005, waikeong chan (the author) said:

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

  4. 4. At 5:16 p.m. on 2 jan 2005, waikeong chan (the author) said:

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

  5. 5. At 9:56 p.m. on 2 jan 2005, Markus Weihs said:

    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
    
  6. 6. At 1:41 a.m. on 3 jan 2005, waikeong chan (the author) said:
    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>

  7. 7. At 4:25 a.m. on 4 jan 2005, Peter Bengtsson said:

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

  8. 8. At 5:20 p.m. on 12 jan 2005, bu66le - said:

    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
    

Sign in to comment