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

[solved] How to separate and keep decimal points for calculations

Started by CreeperGoBoom, 12 December 2018 - 04:54 AM
CreeperGoBoom #1
Posted 12 December 2018 - 05:54 AM
OK this is for something that will accompany a map I am creating, this is part of an API I'm working on, hopefully the comments are self explanatory


function ATMCashOutCount(amount)
  cash[1] = {amount / 100} --[[returns number of items needed to
  give equivalant to x number of $100 bills]]
  cash[2] = --leftover decimal x100 then /50 so I can then figure out remaining change,

  --More change stuff here also requiring leftover decimals from each line

end

So basically, say this calculation equals 5.25. I want the 5.25 to become only 5 and then I want to take the remaining .25 for other calculations (since you cant give 5.25x items)
Edited on 12 December 2018 - 03:20 PM
Bomb Bloke #2
Posted 12 December 2018 - 01:37 PM
Floor your divisions to round down their results. Then use the modulus operator to get the remainder values, which can in turn be divided again for your next unit of currency, and so on.

print("31 divided by 7 is " .. math.floor(31 / 7) .. ", with a remainder of " .. (31 % 7) .. ".")
CreeperGoBoom #3
Posted 12 December 2018 - 04:20 PM
Cheers Bomb Bloke, exactly what I was looking for :D/>, [solved]