# A Very Small Program that Rounds Up Numbers Within Any Decimal Places

• I'm not here to brag but I wrote a very small program that can round up numbers with decimals. It is an extension of BlueTaslem's formula (or something): https://scriptinghelpers.org/questions/4850/how-do-i-round-numbers-in-lua-answered

The program consists of 2 arguments/parameters. The number (x) and the optional argument to round the number up to a certain amount of decimals.

Here is the code:

``````function round(x, decimalPlaces)
decimalPlaces = decimalPlaces or 0 -- decimalPlaces is an optional argument/parameter. Defaults to 0 (round up to an integer).

x = x * (10 ^ decimalPlaces)
x = (x + 0.5 - (x + 0.5) % 1) / (10 ^ decimalPlaces)

return x
end
``````

This function is very useful when calculating Win/Lose ratio (or for me, chance of winning in "%") and game progress (Like when you have completed the game "100%" which is kind of rare to use).

Even though it might seem useless, I still hope people will learn some math in it.

Try typing for example `round(math.pi, 2)` or `round(1.79)` under the function in your script to try it out! • Good job!

There's also this link that has even more examples of rounding functions.

• Cool! Didn't know there are other ways to do this as well • You could do math.ceil() to round up and math.floor() to round down.

• @UltimateRaheem Yeah, but you can't choose what decimal to round too without doing some maths :3

• Rather than just rounding to powers of 10, you can easily round to the nearest multiple of any number

``````local function roundNearest(n, b)
return math.floor(n/b+.5)*b
end
``````

To round to certain decimal places would be like `roundNearest(math.pi, .001)`

• @1waffle1 Would this still work if I change the math.floor() part to math.ceil() if I wanted to round up?

• @UltimateRaheem I think all you would have to do is subtract the .5 instead of add, if you try that.

• @1waffle1 What math class you took to learn all this?

• @UltimateRaheem Actually if you would want to round up you wouldn't use this function, but instead simply using `math.ceil()`, right?

• @OldPalHappy Correct. But what if I wanted to round up to a decimal place?

• @OldPalHappy God dammit. The first solution shown on this page is exactly what I was about to suggest.

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.