I need to round a number up to tenths. Which method is responsible for this?
Pretty sure you are looking for math.floor(num * mult + 0.5) / mult
.
Where num is your number and mult is the multiplier. If you want to round to tenths just put 10^1 (100).
For strings use string.format("%.1f", numberstring)
.
local a = 4.3323456
print(math.floor(a * 10) / 10) --> 4.3
Im lazy to copy what i said before
So here you go
That does work, but it is not rounding.
Unless he needs flooring you need to add + 0.5
to the formula.
The 3 main functions are:
Round - to the closest number: 4.6 → 5 | 4.4 → 4
Floor - to the smaller number: 4.9 → 4
Ceil - to the bigger number: 4.1 → 5
Since most functions in the Lua’s math library were made to work only with whole numbers, you need that extra numbers in the formula.
He asked to make 4.332 into 4.3 in the title. But then he asked how to round a number up in the description… so I’m assuming he just wants to round. You can use my method and change math.floor
to math.round
.
BRUH
you are absolutely right
I am late but the modulo operator is the best method for truncating decimals
Do note this rounds down to infinity, so -4.33234
would give you -4.4
instead of -4.3
. If you use math.fmod
it will round down to zero, which is what OP wants.