So, As of now I have a script that prints a part’s velocity, Only problem is it prints the entire velocity. So like, 7.48973874 I would like to make it simplier Like, Print 7.48973874 as 7 Any way i could achieve this?
You could round the number:
local function Round(number)
return math.floor(number + 0.5)
end
Or you could just use this:
function round(num)
return math.floor(num)
end
You could also replace math.floor()
, with math.ceil()
if you want.
Worked perfect. Thanks!
thirtycharacters
Nezuo’s returns the closest integer and is more precise than just using floor or ceil.
For most cases it is a far better solution to this problem than your simplified version which is just wrapping a simple function call in another function call.
What is wrong with using math.ceil()
or using math.floor()
? They both return the closest value without a decimal.
Floor and Ceil disregard the value of the decimal portion
For example:
math.floor will return 1 if the input is 1.1 or 1.9
math.ceil will return 2 if the input is 1.1 or 1.9
Nezuo’s round function will return 1 if the input is 1.1, or will return 2 if the input is 1.9
The mathematical correct way to round is using Nezuos method.
Just using math.ceil or math.floor will just round up/down to the nearest number, e.g math.ceil(5.12) will return 6 not 5.
math.ceil(number - 0.5) is mostly correct and functions the same except for when the decimal is exactly 0.5 in which case the function would round down whereas in mathematics you would round up.
How does this behaviour work? Since Nezuo is using math.floor, would it not round down either way? How does it round up from 1.9 to 2 if math.floor is used?
Yep this is correct. If he wanted to round to the nearest whole you’d have to do something like this
function round(n)
if n % 1 >= 0.5 then
return math.ceil(n)
end
return math.floor(n)
end
He does the floor of Num+0.5 though.
It will round down, yes. But if your num is .5 or higher, it’ll be rounding down from the next number.
I explained poorly, so just try examples.
1.1 → 1.6 → 1
1.7 → 2.2 → 2
3.9 → 4.4 → 4
This way, you’re getting the proper rounding function.
That’s an… interesting way to round?
Thanks for clarifying though.