so basically I created this script finds the magnitude of this red brick in the work space
On line 16
it prints the studs away from the part but it all comes out in decimals. Is there a way for that decimal to be rounded up to a whole number?
local radiustesting = 20 local torso = script.Parent.Parent:WaitForChild("HumanoidRootPart") local bricktobefound = game.Workspace:WaitForChild("TheRadiusBrick") while true do---the while true do loop that gose on foreever Xd... no LITERALLY...I MEAN "Forever" !....... wait(.1)--put a wait for a certain amount of time so roblox wont crash and die X:D if (torso.Position - bricktobefound.Position).magnitude <= radiustesting then --keep printing this statement print("wow Me BlockDummy is near Brick...oh woahhh....! thats this is Cool!!!") else print("its too far! Studs Away: "..(torso.Position - bricktobefound.Position).magnitude) end end
This will just be iLordOfAviation but explained a bit.
math.floor
takes the mathematical floor of a number - that is, it rounds it down.
4.1 -> 4.0
4.5 -> 4.0
4.9 -> 4.0
5.0 -> 5.0
If we want normal rounding, this isn't quite right, but we can achieve what we want by adding 0.5.
If the number should be rounded down, the decimal part must be less than 0.5. Therefore, adding 0.5 must not push it over to the next whole number.
If the number should be rounded up, the decimal part must be either equal to or greater than 0.5
If greater than 0.5, adding another 0.5 pushes the whole part of the number up by one, with some smaller decimal amount left over. This smaller decimal amount must be less than 0.5.
If equal to 0.5, adding another 0.5 makes the number equal to a whole number. At this point, math.floor
doesn't touch it.
Some examples:
4.1 -> 4.6 -> 4.0
4.5 -> 5.0 -> 5.0
4.9 -> 5.4 -> 5.0
5.0 -> 5.5 -> 5.0
Typically, the rounding functions you'll find in any sort of library will be concerned with getting decimal numbers to integers. So, if we want to round to a certain number of decimals, we have to get a little creative.
For example, if we multiply a number by 10, round it using the bit above, and then divide by 10, what happens?
We end up getting the number rounded to 1 decimal place, which is nearing what we want.
Because we're good programmers, the question becomes "How do we generalise this?". Well let's look at another example to see if we can spot a pattern.
Here, we multiplied and divided by 100, and now our number has two decimal places in it. What's the secret?
10^1 = 10 -> 1 decimal place
10^2 = 100 -> 2 decimal places
10^47 -> 47 decimal places
We basically end up with iLordOfAviation's answer.
function round(num, numDecimalPlaces) local mult = 10^(numDecimalPlaces or 0) return math.floor(num * mult + 0.5) / mult end
The only slight weirdness left is numDecimalPlaces or 0
. This is equivalent to
if numDecimalPlaces ~= nil then mult = 10^numDecimalPlaces else mult = 0 end
function round(num, numDecimalPlaces) local mult = 10^(numDecimalPlaces or 0) return math.floor(num * mult + 0.5) / mult end
With this function;
round(1.5) = 2 round(1.4) = 1 round(1.5, 1) = 1.5 round(1.123, 2) = 1.12
Source: http://lua-users.org/wiki/SimpleRound
So for your case:
function round(num, numDecimalPlaces) local mult = 10^(numDecimalPlaces or 0) return math.floor(num * mult + 0.5) / mult end local radiustesting = 20 local torso = script.Parent.Parent:WaitForChild("HumanoidRootPart") local bricktobefound = game.Workspace:WaitForChild("TheRadiusBrick") while true do---the while true do loop that gose on foreever Xd... no LITERALLY...I MEAN "Forever" !....... wait(.1)--put a wait for a certain amount of time so roblox wont crash and die X:D if (torso.Position - bricktobefound.Position).magnitude <= radiustesting then --keep printing this statement print("wow Me BlockDummy is near Brick...oh woahhh....! thats this is Cool!!!") else print("its too far! Studs Away: "..round(torso.Position - bricktobefound.Position).magnitude) end end
uh
is this what you're looking for?
function round(num) if (tonumber(num)) then return math.floor(num+0.5) --round(4) => 4 --round(4.5) => 5 --round(3.694201337) => 4 --round(-5.3) => -5 end end
if you're looking to just, round it up without ever rounding it down, just use math.ceil(num)