Last active
December 11, 2015 19:28
-
-
Save sammachin/4648722 to your computer and use it in GitHub Desktop.
Convert an amount into the smallest number of notes & coins, currently written for British Currency but should be easy to modify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def toCash(amount): | |
pounds,pence = divmod(amount, 1) | |
pounds = int(pounds) | |
pence = pence*100 | |
pence = int(round(pence)) | |
notes = [50, 20, 10, 5, 1] #These are the whole amounts Yes I know the £1 & £2 are coins in the UK! | |
coins = [50, 20, 10, 5, 2, 1] #These are the less than whole amounts eg the pence | |
for note in notes: | |
count = pounds / note | |
if count >= 1: | |
pounds = pounds - (note * count) | |
if count != 0: | |
print str(count) + " X £" + str(note) #Replace £ with your local currency symbol eg $ € zł etc | |
for coin in coins: | |
count = pence / coin | |
if count >= 1: | |
pence = pence - (coin * count) | |
if count != 0: | |
print str(count) + " X " + str(coin) + "p" #Replace p with your local centesimal currency eg cents, øre, bani, etc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment