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

How to calculate fill percentage of a tank?

Started by Metalhawk2012, 28 May 2015 - 03:33 PM
Metalhawk2012 #1
Posted 28 May 2015 - 05:33 PM
Just a quick one ive made a small program displaying multiple drums/tanks on a monitor displaying the amount and the capacity

but sadly i do not know how to calculate the fill percentage

any help please?
KingofGamesYami #2
Posted 28 May 2015 - 05:35 PM
Percentage is calculated as:

Current Amount / Maximum Amount * 100
Lyqyd #3
Posted 28 May 2015 - 05:37 PM
Full percentage is a pretty simple formula. It's the ratio of the amount to the capacity expressed as a value to 100, or:

amount / capacity = x / 100

Simply multiply both sides by 100 to isolate x and solve:

amount * 100 / capacity = x

Or, in Lua code:


local percentage = amount * 100 / capacity
Metalhawk2012 #4
Posted 28 May 2015 - 05:47 PM
well i feel dumb……Cheers dude it works i was trying a bit more complex code but the simple one works just as good
and Thank you too Lyqyd that code also worked
A bit hard to see but there is 18 drums and is now showing the amount capacity and fill percentage

Edited on 28 May 2015 - 03:48 PM
Metalhawk2012 #5
Posted 28 May 2015 - 08:24 PM
Okay so i got a pretty decent code now but i would also like to ask another question

How do i detect a empty drum without it giving a nil value error?
KingofGamesYami #6
Posted 28 May 2015 - 09:25 PM
Don't assume something is a table until you've checked that it isn't nil.
Bomb Bloke #7
Posted 28 May 2015 - 11:31 PM
Eg.