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

Stupid question - math.ceil

Started by Dustmuz, 21 April 2017 - 12:11 PM
Dustmuz #1
Posted 21 April 2017 - 02:11 PM
im a little lost in my lua programming, not done it for quite some time now, and forgotten half of it :D/>

im making yet another power monitor/reactor controller
this time im trying to draw some power bars on the screen.

but i want to make each "dot" of the bar a 5% increment.

I know how to do the math of it.
But my question is..

How do you get math.ceil to round up to each 5, instead of each full number?

ex math.ceil( 10 / 4 ) – this gives 2.5. normally it would be rounded to 3,
but if i want it rounded to 5 instead.. how do i do that?
Larry84 #2
Posted 21 April 2017 - 02:48 PM
You should just need to divide the percentage by 5, then round it up.
Exerro #3
Posted 21 April 2017 - 03:58 PM
if you want 10/x to round to 5, x should be 2… math.ceil( 10 / 2 )

Ignore me, I misread the question, see InDitTasten's response below.
Edited on 21 April 2017 - 09:44 PM
InDieTasten #4
Posted 21 April 2017 - 07:07 PM
When I understand correctly, you want to round to the next highest half unit:

1.3 → 1.5
1.6667 → 2.0
1.1 → 1.5

In order to do that, you do this:

desiredValue = math.ceil(inputValue * 2) / 2


EDIT: Ok, just reread your question. It seems you want to round to the next highest full values dividable by 5:

30 → 30
31.5 → 35
1.5 → 5

In that case, this would work for you

desiredValue = math.ceil(inputValue / 5) * 5
Edited on 21 April 2017 - 05:16 PM
Dustmuz #5
Posted 24 April 2017 - 12:09 PM
When I understand correctly, you want to round to the next highest half unit:

1.3 → 1.5
1.6667 → 2.0
1.1 → 1.5

In order to do that, you do this:

desiredValue = math.ceil(inputValue * 2) / 2


EDIT: Ok, just reread your question. It seems you want to round to the next highest full values dividable by 5:

30 → 30
31.5 → 35
1.5 → 5

In that case, this would work for you

desiredValue = math.ceil(inputValue / 5) * 5

Thanks this works out :D/>
Edited on 24 April 2017 - 10:16 AM