What do you think about this function which rounds to the nearest integer? I made this myself and it certainly does work, and I’m very proud of it because I made it without any outside help. Do you have any criticisms or any optimizations that may be used to make this a better function?
function round(num) if num%math.floor(num) >= 0.5 then return math.ceil(num) else return math.floor(num) end end
Essentially what it does is take a number (preferably with a decimal) and modulates the floor of itself. For example,
INPUT => 18.3
- You want to round 18.3
- 18.3 goes into the function and the modulus of 18(which is 18.3 rounded down) gives you the number 0.3
- Is 0.3 greater than or equal to 0.5? No.
- No? Round down.
OUTPUT => 18