Lua - Format integer

后端 未结 4 1423
天涯浪人
天涯浪人 2020-12-11 18:27

i\'d like to format a number to look as follows \"1,234\" or \"1,234,432\" or \"123,456,789\", you get the idea. I tried doing this as follows;

function ref         


        
4条回答
  •  鱼传尺愫
    2020-12-11 18:59

    I remember discussing about this in the LÖVE forums ... let me look for it...

    Found it!

    This will work with positive integers:

    function reformatInt(i)
      return tostring(i):reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", "")
    end
    

    On the link above you may read details about implementation.

提交回复
热议问题