just a simple little program that able to check the data key-in is in form of money or not.
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
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:
Hi Markus! yeah! your code is looks smartter Hi Markus! yeah! your code is looks smartter
Hi Markus! yeah! your code is looks smartter Hi Markus! yeah! your code is looks smartter
but it cant function when i key in "123.4". i expect it return $123.40 as the answer; but it return false!!
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:
</pre>
What about , comma. In some countries the default delimiter on decimal value is the , comma character. Not the . dot.
isMoney with re.