How to i limit the number value to not add extra 0 when removing or adding numbers?

Hi, i would like to know how to stop this from happening once a number value gets changed.
image
this is what it looks like when a value gets changed (aka the balance after the user has bought something)

image
This is the purchase prompt which globaly removes the value of the number value in workspace.

image
This is the number value.

This is because NumberValues can store numbers up to 15 digits of precision, but this is okay. What I would do is use a precise rounding function for the TextLabel. Here is one that I made previously:

local function GetTens(number)
	-- Convert number to string
	local decimalString = tostring(number):split('.')[2]
	
	if not decimalString then
		return 1
	-- String is in scientific notation
	elseif decimalString:sub(1, 1) == 'e' then
		local digits = tonumber(decimalString:gsub('%D', ''))
		
		if digits then
			return 10 ^ digits
		else
			return 1
		end
	else
		return 10 ^ #decimalString
	end
end

local function RoundTo(number, multiple)
	local tens = GetTens(multiple)
	local roundingFactor = multiple * tens
	
	return math.round(number * tens / roundingFactor) * roundingFactor / tens
end

--Example: round NumberValue number to nearest hundredth for TextLabel
local number = numberValue.Value
local roundedNumber = RoundTo(number, 0.01)

textLabel.Text = roundedNumber

Ask me if you have any questions

2 Likes

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