方法一
-- 如 1234567 转换为 1,234,567
---@param number 数字
---@param decimalCount 需要保留小数点后的位数不足用0补
function thousandNum(number, decimalCount)
if type(number) ~= "number" then
return number;
end
if number == math.huge then
return tostring(number);
end
local intNum = number;
local floatStr = "";
--如果是小数,需要保留小数点后的数字
local arr = stringExplode(tostring(number), ".");
if Utils.sizeof(arr) > 1 then
intNum = tonumber(arr[1]); --整数部分
floatStr = arr[2]; --小数部分
end
if decimalCount and decimalCount > 0 then
floatStr = string.sub(floatStr, 1, decimalCount);
if sizeof(floatStr) < decimalCount then
-- 小数部位位数不足的,用0补足
for i = 1, decimalCount - sizeof(floatStr) do
floatStr = floatStr .. "0" ;
end
end
end
local ret;
repeat
local num , _ = math.modf(intNum % 1000);
intNum = (intNum - num) / 1000;
local str = tostring(num)
if intNum <= 0 then
str = tostring(num);
end
if ret then
ret = str .. "," .. ret;
else
ret = str;
end
until intNum <= 0;
if Utils.sizeof(floatStr) > 0 then
ret = string.format("%s.%s", ret, floatStr);
end
return ret;
end
---@param t table or string
function sizeof(t)
local n = 0;
if type(t) == "table" then
-- 遍历 table,累加元素个数
for __, __ in pairs(t) do
n = n + 1;
end
elseif type(t) == "string" then
n = string.len(t);
end
return n;
end
-- 将字符串打断
function stringExplode(str, sep)
if sep == nil then
sep = "%s"
end
local t = {};
local i = 1;
for str in string.gmatch(str, "([^"..sep.."]+)") do
t[i] = str;
i = i + 1;
end
return t;
end
方法二
local defaultIntegerSeparator = ','
local defaultDecimalSeparator = '.'
--- Formats a given number to a string
-- param[num] a number value
-- param[decimals] int decimals the number of decimals after the whole part. Defaults to 0 when omitted.
-- param[thousandSeparator] string thousandSeparator the symbol used to separate thousands. Defaults to `,` when not given.
-- param[decimalSeparator] string decimalSeparator the symbol used to separate the whole part from the decimal part. Defaults to `.` when not given.
-- param[sign] string sign the string to be used to replace the minus symbol for negative numbers. Defaults to `-` when omitted.
function numberFormat(num, decimals, thousandSeparator, decimalSeparator, sign)
--num = floor(num)==num and (tostring(num)..'.0') or tostring(num)
local int = floor(num)
local dec = num - int
--local int, dec = num:match('(-*%d+)%.(%d*)')
local fmt, repl_pattern, repl = tostring(int), "%1"..(thousandSeparator or defaultIntegerSeparator).."%2"
while true do
fmt, repl = fmt:gsub("^(-?%d+)(%d%d%d)", repl_pattern)
if (repl==0) then
break
end
end
fmt = sign and (fmt:gsub('^%-',sign)) or fmt
if dec == 0 then--没有小数位
return fmt
end
if decimals == nil then--显示小数位数为空
return fmt
end
local decStr = format("%."..decimals.."f",dec)
decStr = decStr:sub(2,#decStr)
--[[local decStr = tostring(dec)
if #decStr > decimals then
decStr = tonumber(decStr:sub(decimals+1,decimals+1))>5
and tostring(tonumber(dec:sub(1,decimals))+1)
or decStr:sub(1,decimals)
else
decStr = StrUtils.rpad(decStr,decimals,'0')
end--]]
return fmt..decStr
end