Vector3 Values become very long?

Hi, I was making this thing where you have to type in a vector3 value and it will be stored as one. It works well but I was wondering why Roblox makes float values (especially short ones) incredibly long for no reason.
Because converting this into text for a textbox makes the text wayyyyy too long. Anyway to stop this or make it shorter? :skull:
image

They are incredibly long because of precision errors with floats, a computer-based problem that is very hard to fix but possible to mitigate. It simply cannot find the right binary combination to reach exactly that number, and therefore resorts to the closest representation.

Alright, is there a way that I don’t have to print that many numbers all at once?

You can try to format the string it converts from the numbers to limit the numbers like that as a workaround. I think it is string.format(), works like regular expressions.

Is there a way for me to know what index the decimal in a string is? Like python where you can use a function to find the index of the first specific instance of the string?

You can probably try using string.find. The string library has all of it.

If you want to format a vector3, something like this should do the trick:

function vector3formatter(maxDecimals)
    local d = maxDecimals
    local formatString = ("(%%.%dg, %%.%dg, %%.%dg)"):format(d, d, d)
    return function(v3)
        return formatString:format(v3.X, v3.Y, v3.Z)
    end
end

local v3f20d = vector3formatter(20)
local v3f10d = vector3formatter(10)
print(v3f20d({X=0.3, Y=1/10, Z=0})) --(0.2999999999999999889, 0.10000000000000000555, 0)
print(v3f10d({X=0.3, Y=1/10, Z=0})) --(0.3, 0.1, 0)

Or a simpler one that just uses a fixed number of decimals:

function formatVector3(v3)
    return ("(%.10g, %.10g, %.10g)"):format(v3.X, v3.Y, v3.Z)
end

that’s just how float-rounding works, you can try something like:

local function toDecimalPlaces(_string: string, places: number)
	if not _string then return end
	if not places then places = 0 end

	local before = _string:match(".*%."):gsub("%.", "") -- Get everything before the `.` and remove the `.` from it
	local after = _string:match("%..*"):gsub("%.", "") -- Get everything after the `.` and remove the `.` from it

	if places == 0 then return before end

	return `{before}.{after:sub(1, places)}`
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.