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

math.floor() not working?

Started by qwerty, 06 August 2018 - 02:06 PM
qwerty #1
Posted 06 August 2018 - 04:06 PM
It seams that math.floor() has stopped woking. I'm currently working on a controller for a nuclear reactor from extreme reactors and i need to get a percentage of the stored energy so i tried math.floor() but that always outputs 0.
How i get the two inputs is as follows:
r = peripheral.wrap("bottom")
cap = r.getEnergyCapacity()
strd = r.getEnergyStored()

Then i tried math.floor(strd / cap) and even added tostring() before printing but it allways outputs a 0.
Note: when i print the cap/strd they allways return their intended values so i guess I just don't know how to use math.floor()
qwerty #2
Posted 06 August 2018 - 04:19 PM
Never mind i used "/" instead of ","

I feel so stupid…

But still, how does one calculate percentages in lua?
Dog #3
Posted 06 August 2018 - 04:57 PM
I believe what you are looking for is math.floor((strd / cap) * 100)

Since math floor rounds down to the nearest whole number, strd/cap rounds down to zero unless it's 100%, then it will be 1.

strd/cap will give you a number of 1 or less (e.g. 0.50) - then you multiply that times 100 to get your percentage (e.g. 50%)

If the number after the decimal you're multiplying is more than two digits (e.g. 0.125), you'll get a percentage with a decimal (e.g. 12.5%). Use math.floor() to round down to the nearest whole percentage (e.g. 12%). This is especially useful for long numbers like 25.12578249%, which would round down to 25%.
Edited on 06 August 2018 - 03:16 PM