访客地图

Lua千分位格式化数字

张成
2015/1

function number_format(num,deperator)
    local str1 =""
    local str = tostring(num)
    local strLen = string.len(str)
       
    if deperator == nil then
        deperator = ","
    end
    deperator = tostring(deperator)
       
    for i=1,strLen do
        str1 = string.char(string.byte(str,strLen+1 - i)) .. str1
        if math.mod(i,3) == 0 then
            --下一个数 还有
            if strLen - i ~= 0 then
                str1 = ","..str1
            end
        end
    end
    return str1
end



上一篇:

下一篇: