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

How do I shorten a decimal place without rounding the number?

Hello! I am having trouble shortening decimals places. The methods I've used are

math.floor,
string.format("%0.d, %0.d, %0.d", [value set] ), 
:split(",")

The methods above did shorten the decimal, BUT it rounded the number up which is what I don't want to happen. I am trying to print the position of a part in workspace and I get the following:

print(workspace.Part.Position)

output:

-134.350006, 17.5000114, 165.799988

I need to keep the number before the decimal the same due to precision. My desired outcome from this:

-134.350, 17.500, 165.799

All (serious) answers appreciated!

1 answer

This is a fairly simple problem. I suggest working with the numbers as strings and using string.sub(). You would have something like:

local coordinate = -134.350006
print(tonumber(string.sub(tostring(coordinate), 1, 7)))

This will print the characters of the number from the first character to the 7th. You should add some checks that adjust the length (currently 7) depending on how long the number before the decimal is, if it's negative, etc. to account for the number of characters before the decimal.

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