Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I round numbers?

I'm making a script that uses an equation to calculate the distance between 2 objects, then moves 1 object to the other in a certain Time, and the speed at which it goes to the other part is dependent on the Time. Problem is, these equations will often give numbers with a crazy amount of numbers after the decimal, this causes all sorts of weirdness, such as the part missing its target. How would I round up the numbers so I don't get such crazy numbers?

--Cup goes to position 1
local Time=4
			local Distance=(math.abs(Cup.Position.X+Pos1.Position.X))
			local StudsPer=(Time/Distance)
			local TimeUp=0
			------------
			repeat
				wait(StudsPer)
				Cup.CFrame=Cup.CFrame*CFrame.new(0,0,StudsPer)
				TimeUp=TimeUp+StudsPer
				
			until
				TimeUp>=Time
				do Cup.Position=Pos1.Position--Ensures it gets there in case of bugs

4 comments

0

Just use math.floor() it rounds any number up!

0

math.ceil()*

0

guss i shall explain kaboobs

2 answers

✓ Accepted answer

Rounding numbers is easy,

local n = 1.5
print(math.ceil(n))

will output:

2

and

local n = 1.5
print(math.floor(n))

will output:

1

Sources: math.floor

math.ceil

There are two methods to rounding the two are, math.ceil() and math.floor(). as you can imagine ceil is short for ceiling so it rounds up, and floor is well... floor, so it rounds down. These are very simple to understand.

Example:

local rounddown = 12.5
local roundup = 12.5

print(math.ceil(roundup)) --prints 13
print(math.floor(rounddown)) --prints 12

1 comment

0

Is there a way to only round the first demical Numer? So like 12.5667747547474754754 would become 12.6?

This discussion is preserved as it was. New answers, comments, and votes are closed.