I need something like this that works normal, instead of rounding one way. For example:
print(math.floor(4.2)) returns 4. print(math.ceil(4.2)) returns 5.
Is there a function or a way so that it rounds up or down accordingly?
Example:
1.2 --> 1
2.5 --> 3
3.7 --> 4
Simply before rounding down, add 0.5 to the number, if you want to round up, take 0.5 from the number. Here's some examples:
-- Using math.floor(): -- 1.4 - Rounds down to 1 print(1.4 + 0.5) -- 1.9 print(math.floor(1.4 + 0.5)) -- 1 -- 1.6 - Rounds up to 2 print(1.6 + 0.5) -- 2.1 print(math.floor(1.6 + 0.5)) -- 2 -- Using math.ceil(): -- 2.4 - Rounds down to 2 print(2.4 - 0.5) -- 1.9 print(math.floor(2.4 - 0.5)) -- 2 -- 2.6 - Rounds up to 3 print(2.6 - 0.5) -- 2.1 print(math.floor(2.6 - 0.5)) -- 3
Hope you understand and it works for you!