How can make my text label show accurate abbreviations like shown in the description

How can make my text label show accurate abbreviations like shown in the below

image

2 Likes

I like this question. This might not be the most efficient approach but it’s how I’d do it. You can do this by splitting the initial number’s string and then every 3 characters, add a separator. In this case it’s a comma.

    local separator = ',' -- Change this to whatever you want
	local function abbreviate(number) 
		local numberString = tostring(number) 
		local splitString = string.split(numberString, '') 
		local compiledString = '' -- We need a base. Nil won't work because it's not a string and gives an error if attempted to be concatenated with a string.
		local counter = 0 -- Accumulator so we know where to add the separator
		for i = #splitString, 1, -1 do 
			counter += 1 
			if (counter % 3 == 1) and (i ~= #splitString) then -- Just ensures that no extra comma is added to the beginning or end of your string.
				compiledString = separator..compiledString -- If the counter is equal to 3 then add the separator
			end 
			compiledString = splitString[i]..compiledString -- Concatenate the next number with the string compiled so far
		end 
		return compiledString
	end 
	print(abbreviate(123456132)) --> 123,456,132

how do I make it show the amount of cash I have

Perhaps create a listener that listens for a given IntValue to change, then whenever it changes update the label.

Here’s an example:

local intValue = Instance.new('IntValue')

local separator = ',' -- Change this to whatever you want
local function abbreviate(number) 
	local numberString = tostring(number) 
	local splitString = string.split(numberString, '') 
	local compiledString = '' -- We need a base. Nil won't work because it's not a string and gives an error if attempted to be concatenated with a string.
	local counter = 0 -- Accumulator so we know where to add the separator
	for i = #splitString, 1, -1 do 
		counter += 1 
		if (counter % 3 == 1) and (i ~= #splitString) then -- Just ensures that no extra comma is added to the beginning or end of your string.
			compiledString = separator..compiledString -- If the counter is equal to 3 then add the separator
		end 
		compiledString = splitString[i]..compiledString -- Concatenate the next number with the string compiled so far
	end 
	return compiledString
end 

local function updateLabel()
    script.Parent.Text = abbreviate(intValue.Value)
end

intValue:GetPropertyChangedSignal('Value'):Connect(updateLabel)
1 Like

Here is another formula that is a little simpler. Is the leaderstats named “Cash”? If so then put this script as a child of the textlabel.

function comma_value(amount)
  local formatted = amount
  while true do  
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
    if (k==0) then
      break
    end
  end
  return formatted
end

local cash = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Cash")
cash.Changed:Connect(function()
      script.Parent.Text = "Cash :"..comma_value(cash.Value)
end)
1 Like