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

[Lua][Question]How do I round up/down a number?

Started by Frederikam, 08 May 2013 - 03:44 AM
Frederikam #1
Posted 08 May 2013 - 05:44 AM
I am trying to make a graph api, where I will use the following formula to determine where to draw a pixel for a column:

number / (scaleFromUserInput / monitorHeight)

The output of this should output a rough position, but the problem is that I could get something like "81.98975". Is there a function or anything else I can use to round this down or even up?
Shnupbups #2
Posted 08 May 2013 - 05:46 AM
math.ceil(number) and math.floor(number) rounds up and down respectively.
Frederikam #3
Posted 08 May 2013 - 05:50 AM
math.ceil(number) and math.floor(number) rounds up and down respectively.

Oh, I wonder how I didn't see that in the reference manual. Oh well thanks.

Result:

historyIndexed[i] = math.ceil(historyRaw[i] / scaleDivHeight)
digpoe #4
Posted 08 May 2013 - 09:42 AM
You can use this:


math.floor(num + 0.5)

It rounds 'num' up if it has a decimal higher than .5, and rounds down if it doesn't.