Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I round numbers?

Asked by
SuperFryX 130
8 years ago

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
0
Just use math.floor() it rounds any number up! docrobloxman52 407 — 8y
0
math.ceil()* dyler3 1510 — 8y
0
guss i shall explain kaboobs NotSoNorm 777 — 8y

2 answers

Log in to vote
1
Answered by
drahsid5 250 Moderation Voter
8 years ago

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

Ad
Log in to vote
1
Answered by
NotSoNorm 777 Moderation Voter
8 years ago

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
0
Is there a way to only round the first demical Numer? So like 12.5667747547474754754 would become 12.6? SuperFryX 130 — 8y

Answer this question