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

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

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

Ad

Answer this question