This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
pb2007's profile picture

Help with exchanges and math

Started by pb2007, 21 November 2017 - 10:17 PM
pb2007 #1
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?
Bomb Bloke #2
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
pb2007 #3
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.
Bomb Bloke #4
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.")