项目需要,需将任意的阿拉伯数字以中文字符的方式转换展示,这里记录一下
主要实现的逻辑:
1,定义2张初始表:
tbl1 = {[0] = "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}
tbl2 = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "兆", "十", "百", "千"}
对应位数上的数字是多少显示多少,位数的单位是多少加多少,类似:
1024 ==> 一千零百二十四;
100001==>一百零十零千零百零十一;
个位数的单位以空字符串处理。
2,中间遇到多个零的情况只保留一个。
3,进制数处理“万”,“亿”等,每4档=0的情况,要手动设置单位进制。
function convert_arab_to_chinese(number)
assert(tonumber(number), "传入参数非正确number类型!")
local numerical_tbl = {}
local numerical_names = {[0] = "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}
local numerical_units = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "兆", "十", "百", "千"}
--01,数字转成表结构存储
local numerical_length = string.len(number)
for i = 1, numerical_length do
numerical_tbl[i] = tonumber(string.sub(number, i, i))
end
--02,对应数字转中文处理
local result_numberical = ""
local to_append_zero, need_filling = false, true
for index, number in ipairs(numerical_tbl) do
--从高位到底位的顺序数字转成对应的从低位到高位的顺序数字单位.
local real_unit_index = numerical_length - index + 1
if number == 0 then
if need_filling then
if real_unit_index == 5 then--万位
result_numberical = result_numberical .. "万"
need_filling = false
end
if real_unit_index == 9 then--亿位
result_numberical = result_numberical .. "亿"
need_filling = false
end
if real_unit_index == 13 then--兆位
result_numberical = result_numberical .. "兆"
need_filling = false
end
end
to_append_zero = true
else
if to_append_zero then
result_numberical = result_numberical .. "零"
to_append_zero = false
end
result_numberical = result_numberical .. numerical_names[number] .. numerical_units[real_unit_index]
if real_unit_index == 5 or real_unit_index == 9 or real_unit_index == 13 then
need_filling = false
else
need_filling = true
end
end
end
return result_numberical
end