Jump to content




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


2 replies to this topic

#1 CreeperGoBoom

  • Members
  • 91 posts

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 by CreeperGoBoom, 12 December 2018 - 04:20 PM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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) .. ".")


#3 CreeperGoBoom

  • Members
  • 91 posts

Posted 12 December 2018 - 04:20 PM

Cheers Bomb Bloke, exactly what I was looking for :D, [solved]





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users