[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to format a number with thousandth separators?
- From: Wolfgang Oertl <wolfgang.oertl@...>
- Date: Wed, 25 Jan 2006 21:21:21 +0100
The Lua printf equivalent, string.print, does not support
thousand separators. So the only way is to do it manually, like
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")
.. string.sub(string.format("%.2f", v - math.floor(v)), 2)
end
print(format_thousand(1234567.89))
There may be a more elegant way, but that's the best I could come up
with!
Wolfgang