数字格式化

lua-users home
wiki

以下实现提供了一个名为“format_num”的函数,允许将数字格式化为货币值,例如将 12000000 格式化为 $12,000,000.00。这在报告中格式化数值时很有用。

下方有其他代码。

-- from sam_lie
-- Compatible with Lua 5.0 and 5.1.
-- Disclaimer : use at own risk especially for hedge fund reports :-)

---============================================================
-- add comma to separate thousands
-- 
function comma_value(amount)
  local formatted = amount
  while true do  
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
    if (k==0) then
      break
    end
  end
  return formatted
end

---============================================================
-- rounds a number to the nearest decimal places
--
function round(val, decimal)
  if (decimal) then
    return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  else
    return math.floor(val+0.5)
  end
end

--===================================================================
-- given a numeric value formats output with comma to separate thousands
-- and rounded to given decimal places
--
--
function format_num(amount, decimal, prefix, neg_prefix)
  local str_amount,  formatted, famount, remain

  decimal = decimal or 2  -- default 2 decimal places
  neg_prefix = neg_prefix or "-" -- default negative sign

  famount = math.abs(round(amount,decimal))
  famount = math.floor(famount)

  remain = round(math.abs(amount) - famount, decimal)

        -- comma to separate the thousands
  formatted = comma_value(famount)

        -- attach the decimal portion
  if (decimal > 0) then
    remain = string.sub(tostring(remain),3)
    formatted = formatted .. "." .. remain ..
                string.rep("0", decimal - string.len(remain))
  end

        -- attach prefix string e.g '$' 
  formatted = (prefix or "") .. formatted 

        -- if value is negative then format accordingly
  if (amount<0) then
    if (neg_prefix=="()") then
      formatted = "("..formatted ..")"
    else
      formatted = neg_prefix .. formatted 
    end
  end

  return formatted
end

示例用法

amount = 1333444.1
print(format_num(amount,2))
print(format_num(amount,-2,"US$"))

amount = -22333444.5634
print(format_num(amount,2,"$"))
print(format_num(amount,2,"$","()"))
print(format_num(amount,3,"$","NEG "))

输出

1,333,444.10
US$1,333,400
-$22,333,444.56
($22,333,444.56)
NEG $22,333,444.563

RichardWarburton 替代版本

function comma_value(n) -- credit http://richard.warburton.it
	local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
	return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

示例用法

print(comma_value(9))
print(comma_value(999))
print(comma_value(1000))
print(comma_value('1333444.10'))
print(comma_value('US$1333400'))
print(comma_value('-$22333444.56'))
print(comma_value('($22333444.56)'))
print(comma_value('NEG $22333444.563'))

输出

9
999
1,000
1,333,444.10
US$1,333,400
-$22,333,444.56
($22,333,444.56)
NEG $22,333,444.563

最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2018 年 8 月 23 日下午 1:12 GMT (差异)