This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
madsroge's profile picture

[LUA] thousand separator

Started by madsroge, 04 January 2013 - 12:32 AM
madsroge #1
Posted 04 January 2013 - 01:32 AM
Hello everyone.

I am looking for a code that can add thousand separators to a string or a number. I have found this code.

Code:
Spoilerfunction format_thousand(v)
local s = string.format("%d", math.floor(v))
local pos = string.len(s) % 3
if pos == 0 then pos = 3 end
return string.sub(s, 1, pos)
.. string.gsub(string.sub(s, pos+1), "(…)", ",%1")
.. string.sub(string.format("%.2f", v - math.floor(v)), 2)
end

print(format_thousand(1234567.89))

The problem is that i dont want to have any decimals and my thousand separator should be . instead of ,

can somebody help me please :)/>?
remiX #2
Posted 04 January 2013 - 01:38 AM
Just remove the .89 from the print

Try this?
function format_thousand(v)
    local s = string.format("%d", math.floor(v))
    local pos = string.len(s) % 3
    if pos == 0 then pos = 3 end
    return string.sub(s, 1, pos)
    .. string.gsub(string.sub(s, pos+1), "(...)", ".%1")
end


print(format_thousand(123456789))
madsroge #3
Posted 04 January 2013 - 01:46 AM
Thak you :)/> that solved the problem :D/>