lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



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/