This is my second problem with float numbers
My problem is just that math.round() does not work correctly. math.round() changes my numbers to integers, example: math.round(1.2,2) → 1
math.round() doesn’t have a second parameter, that 2 is never used. You can multiply by 10, round, and divide by 10. Or you can use string.format("%0.1f", number) (which rounds decimals, with the number after the dot specifying decimal places) and convert the return back to a number.
Ah the intricacies of floating point numbers!
For those who don’t know why this even occurs with numbers in games or programs for that matter, just computers in general: computers really aren’t the best with integers because numbers on computers have to be stored in bits (which there’s only a finite allocation for) so you get these sort of issues that look like this:
3 - 1.1 = 1.899978
This mainly happens because some bits have to be cut off to store a number in the floating point format, or else they would not fit in the 32/64 bit arrangement they’re given. This problem can usually be solved by rounding it to the nearest whole, but if you’re looking for a decimal placement rounding you could make your own round function:
local function round(x: number, depth: number): number
depth = 10^(depth - 1)
return math.round(x * depth) / depth
end
round(3 - 1.1, 2) --> 1.9