Wednesday, 11 September 2013

Advice and feedback on dividing cash amounts into actual counts of various bills and coinage

Advice and feedback on dividing cash amounts into actual counts of various
bills and coinage

So I need an idea of how to divide out an amount of money into actual
counts of various bills and coinage. I know this is confusing, so let me
give an example:
$16.32 - Sixteen dollars and thirty-two cents
One $10 bill
One $5 bill
One $1 bill
One Quarter ($0.25)
One Nickel ($0.05)
Two Pennies ($0.01)
So as you can see, we're just getting the number of bills and coinage that
goes into a value, which will change according to user input.
Here's my current setup (Visual Basic):
If 100 Mod amount < 0 Then
If 50 Mod amount < 0 Then
' Continue this pattern until you get all the way down to the end
($0.01)
Else
While amount > 50
fiftiesAmount += 1
amount -= 50
End If
Else
While amount > 100
hundredsAmount += 1
amount -= 100
End If
Basically, each If statement determines whether or not your total amount
needs an extra billing amount of that type, and then either adds to the
amount of bills/coinage already created or moves on to the next amount.
Is this an efficient way of doing things, or am I missing out on an
easier/faster algorithm/pattern that would make my life, and whoever is
reading my code's life easier? If you need extra details, I'll be happy to
edit the question as needed.

No comments:

Post a Comment