其他分享
首页 > 其他分享> > lua阿拉伯数字转化成汉字

lua阿拉伯数字转化成汉字

作者:互联网

不废话直接上代码

--阿拉伯数字汉字化
function game.numberToChinese(number)
    if type(number) ~= "number" then
        return
    end
    local numerical_table = {}
    local numerical_names = {[0] = "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}
    local numerical_units = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "兆", "十", "百", "千"}
 
    --数字转成表结构存储
    local numerical_length = string.len(number)
    for i = 1, numerical_length do
	numerical_table[i] = tonumber(string.sub(number, i, i))
    end
    --对应数字转中文处理
    local result_numberical = ""
    local to_append_zero, need_filling = false, true
    for index, number in ipairs(numerical_table) 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

 

标签:real,index,end,转化成,阿拉伯数字,numerical,lua,result,numberical
来源: https://blog.csdn.net/pyf_914406232/article/details/113631475