Could have shortened my code using a loop, but anybody could do that. Writing it out long hand like that shows the concept.
As flying saucer said, if you are using actual decimal places you might want to floor things out first. Although really, all you need to do is take the number, get the modulo of one, and then from there everything else is already integers so you don't need to floor anything else after that. Just use div 1000 after that.
Edit: This is the same principle behind currencies in games like World Of Warcraft, but it occurred to me this is
not how currency is handled in the US. Once I realized that, it became clear also that it's better to work from the top down, not the bottom up, which is what the rest of these are doing. Consider US currency.
Code:
bills[0] = 100;
bills[1] = 50;
bills[2] = 20;
bills[3] = 10;
bills[4] = 5;
bills[5] = 1;
bills[6] = 50;
bills[7] = 25;
bills[8] = 10;
bills[9] = 5;
var cash; cash[10] = 0;
var change = amount mod 1 * 100;
var dollars = amount div 1;
for(var i=0; i<10; i++;)
if i < 6
{
cash[ i ] = dollars div bills[i];
dollars = dollars mod bills[i];
}
else
{
cash[i] = change div bills[i];
change = change mod bills[i];
}
cash[10] = change;