6 posts
Posted 21 November 2017 - 11:17 PM
I'm trying to make a bank in ComputerCraft like everyone does. I need to split one number (the money) into 4 numbers (diamond count, gold count, iron count, and an ignored number which won't be deposited). My exchange rate is $1000 = 1 diamond, $100 = 1 gold ingot and $10 = 1 iron ingot. I've tried and tried, but I can't get the numbers correct.
How would I do the math code so diamonds = the number of diamonds needed, gold = the number of gold needed and iron = the number of iron needed?
7083 posts
Location
Tasmania (AU)
Posted 22 November 2017 - 12:41 AM
Let's say you had $3456, and wanted to know how many diamond's that's worth. You'd first divide by a thousand, and round down:
money = 3456
diamonds = math.floor( money / 1000 )
To see how much money is left, it's easiest to use the
modulus operator:
money = money % 1000
From there, you should be able to see how to then figure out how much gold and iron you should have.
Edited on 21 November 2017 - 11:42 PM
6 posts
Posted 29 November 2017 - 06:18 PM
No, I actually can't see how I would figure out how much gold and iron I would have. I think my gold would be like this:
gold = diamonds - math.floor(money - 100)
I think the iron would be like this, but I'm not sure:
iron = gold - math.floor(money - 10)
Can you just correct me and tell me how to do it? Thanks.
7083 posts
Location
Tasmania (AU)
Posted 29 November 2017 - 10:44 PM
money = 3456
diamonds = math.floor( money / 1000 )
money = money % 1000
gold = math.floor( money / 100 )
money = money % 100
iron = math.floor( money / 10 )
money = money % 10
print("I have " .. diamonds .. " diamonds, " .. gold .." gold, " .. iron .. " iron, and $" .. money .. " left over.")