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

Is it Possible to Print a Rounded Value?

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?

3 comments

0

doesnt math.floor() have something to do with that?

0

also i've never had this problem, but i've never used tostring, so maybe remove just tostring and see what happens.

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?

1 answer

✓ Accepted answer

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

2 comments

1

Makes sense. Also you can use math.ceil() to round up. Thanks for the explanation and link!

0

No problem, good luck in your projects!

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