-
Notifications
You must be signed in to change notification settings - Fork 0
/
dollar amount.py
69 lines (58 loc) · 2.38 KB
/
dollar amount.py
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
58
59
60
61
62
63
64
65
66
67
68
69
'''
Reddit r/dailyprogrammer Challenge #330
https://www.reddit.com/r/dailyprogrammer/comments/6yep7x/20170906_challenge_330_intermediate_check_writer/
'''
__author__ = 'Zack Allen'
def int_to_string(input_number):
"""
Takes in an integer from between 0-999 and returns a string representation of the integer in english.
"""
#Array indices match value that will be found from modulus
ones = ['','one','two','three','four','five','six','seven','eight','nine']
teens = ['','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
tens = ['','ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']
int_string = "" #English words will be appended to this string
if input_number == 0:
return "zero "
#Hundred section
if input_number > 99:
int_string += ones[(input_number-input_number%100)/100] + " hundred "
pass
#Tens
if input_number%100 > 19 or input_number%100 == 10:
int_string += tens[input_number%100/10] + " "
#Teens
if input_number%100 > 10 and input_number < 20:
int_string += teens[input_number%10] + " "
#Ones place
elif input_number%10 > 0:
int_string += ones[input_number%10] + " "
pass
return int_string
input_string = str(input("Enter a dollar amount:\n")) #User prompt
output_string = "" #All output will be appended to this string
#separate dollars and cents and thousands
if '.' in input_string:
split_values = input_string.split(".")
dollars = int(split_values[0])
cents = int(split_values[1])
#No cents value
else:
dollars = int(input_string)
cents = 0
if dollars >999999 or dollars <0 or cents >99 or cents <0: #Check that dollar value is in range
print "Invalid dollar/cents amount."
exit(1)
thousands = (dollars -dollars%1000)/1000
hundreds = dollars%1000
if thousands >0 and hundreds >0:
output_string += int_to_string(thousands) + "thousand, "
output_string += int_to_string(hundreds) + "dollars and "
elif thousands >0 and hundreds == 0:
output_string += int_to_string(thousands) + "thousand dollars and "
elif thousands == 0:
output_string += int_to_string(hundreds) + "dollars and "
output_string += int_to_string(cents) + "cents." #cents output
output_string = output_string[0].capitalize() + output_string[1:] #Fixes first letter of any string
print output_string
exit(0)