Jump to content




Stupid question - math.ceil

lua

  • You cannot reply to this topic
4 replies to this topic

#1 Dustmuz

  • Members
  • 174 posts
  • LocationDenmark

Posted 21 April 2017 - 01: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?

#2 Larry84

  • Members
  • 51 posts
  • LocationItaly

Posted 21 April 2017 - 01:48 PM

You should just need to divide the percentage by 5, then round it up.

#3 Exerro

  • Members
  • 801 posts

Posted 21 April 2017 - 02: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 by Exerro, 21 April 2017 - 10:44 PM.


#4 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 21 April 2017 - 06: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 by InDieTasten, 21 April 2017 - 06:16 PM.


#5 Dustmuz

  • Members
  • 174 posts
  • LocationDenmark

Posted 24 April 2017 - 11:09 AM

View PostInDieTasten, on 21 April 2017 - 06:07 PM, said:

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 by Dustmuz, 24 April 2017 - 11:16 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users