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

Is it Possible to Print a Rounded Value?

Asked by
RAYAN1565 691 Moderation Voter
8 years ago
local a = script.Parent

for i=1, 10 do
    a.Transparency = a.Transparency+0.1
    wait(0.2)
end

print("The transparency of the brick is " .. tostring(a.Transparency))

Output: The transparency of the brick is 1.0000001192093

Is it possible to tell the computer to round the value rather than have all these significant figures?

0
doesnt math.floor() have something to do with that? iFireLazer 35 — 8y
0
also i've never had this problem, but i've never used tostring, so maybe remove just tostring and see what happens. iFireLazer 35 — 8y
1
You're right. I just searched the wiki. There's math.floor() and math.ceil(). Is there a function that rounds based on the following significant figure though rather than to the highest integer or lowest integer? RAYAN1565 691 — 8y

1 answer

Log in to vote
1
Answered by
Necrorave 560 Moderation Voter
8 years ago

Using math.floor() you can round Down whatever you wish to inside the parenthesis.

There is a simple method to allow it to round to the nearest whole number though. Simply add .5 to whatever you wish to round up within the math.floor method.

Example:

print(math.floor(2.2 + .5)) -- This should print 2

print(math.floor(2.8 + .5)) -- This should print 3

Using your script:

local a = script.Parent

for i=1, 10 do
    a.Transparency = a.Transparency+0.1
    wait(0.2)
end

print("The transparency of the brick is " .. tostring(math.floor(a.Transparency + .5)))

Let me know if you have any other questions!

Resources:

Math methods

1
Makes sense. Also you can use math.ceil() to round up. Thanks for the explanation and link! RAYAN1565 691 — 8y
0
No problem, good luck in your projects! Necrorave 560 — 8y
Ad

Answer this question