lua 实现数字转换千分法描述的字符串

方法一

-- 如 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值