阿拉伯数字与中文转换

function chs2num(s)----------------数字转换
   local cur    = 0
   local yi    = 0
   local sgl    = 0
   local len    = string.len(s)
   local tt = {}
   tt["零"] = function() end
   tt["一"] = function() sgl = 1   end
   tt["二"] = function() sgl = 2   end
   tt["三"] = function() sgl = 3   end
   tt["四"] = function() sgl = 4   end
   tt["五"] = function() sgl = 5   end
   tt["六"] = function() sgl = 6   end
   tt["七"] = function() sgl = 7   end
   tt['八'] = function() sgl = 8   end
   tt["九"] = function() sgl = 9   end
   tt["十"] = function()
      if sgl == 0 then sgl = 1 end
      cur = cur + sgl * 10
      sgl = 0
   end
   tt["百"] = function()
      cur = cur + sgl * 100
      sgl = 0
   end
   tt["千"] = function()
      cur = cur + sgl * 1000
      sgl = 0
   end
   tt["万"] = function()
      cur = (cur +sgl) * 10000
      sgl = 0
   end
   tt["亿"] = function()
      yi = (cur + sgl) * 100000000
      cur = 0
      sgl = 0
   end
   for i = 1,len,2 do
      k = string.sub(s,i,i+1)
      tt[k]()
   end
   num = yi + cur +sgl
----   print(num)
   return num
end
function chinese_to_number(str)
        if (#str % 2) == 1 then
                str = string.sub(str,1,#str-1);
        end
        result=0
        wan=1
        unit=1
        for i=#str -2 ,0,-2 do
                char=string.sub(str,i+1,i+2)
                if (char=="十") then
                        unit=10*wan
                        if (i==0) then
                                result=result+unit
                        elseif _nums[string.sub(str,i-1,i)]==nil then
                                result=result+unit
                        end
                elseif (char=="甲" and string.sub(str,i+3,i+4)=="子") then
                        unit=60*wan
                        wan=60
                elseif (char=="百") then
                        unit=100*wan
                elseif (char=="千") then
                        unit=1000*wan
                elseif (char=="万") then
                        unit=10000*wan
                        wan=10000
                else
                        if _nums[char]~=nil then
                                result=result+_nums[char]*unit
                        end
                end
        end
        return result
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值