I have a function which takes a number and puts commas in between thousands. So 2389 for example would be formatted to 2,389.
local function formatThousands(num)
local formatted, replacements = string.gsub(tostring(num), "^(-?%d+)(%d%d%d)", "%1,%2");
return replacements < 1 and num or formatted;
end
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
You’ll need to loop over your string with the same pattern until you can’t do any more replacements. Your edits to this were unnecessary.
EDIT: looking at the page again, there’s an even shorter (but more confusing) version of the function in a reply:
function comma_value(n) -- credit http://richard.warburton.it
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
I am not sure about that function, but wouldn’t it be easier to just take a number, convert it to a string, and then add a comma after every 4th number?
local Table = {__index = table}
function addCommas(str)
str = str:reverse()
local sections = setmetatable({}, Table)
for section in str:gmatch '%d%d%d' do
sections:insert(section)
end
local missingLen = #str - 3 * #sections
if missingLen > 0 then
sections:insert(str:sub(-1, -missingLen))
end
return sections:concat(','):reverse()
end
The problem with the code presented on the page is that you may need to call it multiple times, feeding the previous results into it, to get the desired end result when it wasn’t able to find any captures.