[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to format a number with thousandth separators?
- From: PA <petite.abeille@...>
- Date: Wed, 25 Jan 2006 18:04:27 +0100
On Jan 25, 2006, at 09:40, Zakaria wrote:
I'm a Lua newbie. What is the best way to do it?
Very brutal, there must be a better way :)
function self:formatNumber( aNumber, aLocale )
if aNumber ~= nil then
local aString = tostring( math.floor( aNumber ) )
if aString:len() > 3 then
aString = aString:reverse()
aString = aString:gsub( "(%d%d%d)", "%1," )
aString = aString:reverse()
if aString:sub( 1, 1 ) == "," then
aString = aString:sub( 2 )
end
end
return aString
end
return nil
end
Some alternatives:
http://www.bigbold.com/snippets/posts/show/693
http://www.rubygarden.org/ruby?NumericFormat
Cheers
--
PA, Onnay Equitursay
http://alt.textdrive.com/