模組:NumberToChinese
外观
-- See the template documentation or any example for how it is used and works.
local p = {}
local origArgs
local yesno = require('Module:Yesno')
--定義中文數字
local deputy = {"","十","百","千"}
local deputybig = {"","拾","佰","仟"}
local main = {"","萬","億","兆","京","垓","秭","穰","溝","澗","正","載","極","恆河沙","阿僧祇","那由他","不可思議","無量大數"}
local numberch = {"〇","一","二","三","四","五","六","七","八","九"}
local numberbig = {"零","壹","貳","參","肆","伍","陸","柒","捌","玖"}
function p.Number_to_Chinese(frame)
-- For calling from #invoke.
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
else
origArgs = frame
end
local pframe = frame:getParent()
local args = pframe.args
if (origArgs['num'] and origArgs['num'] ~= '') then
num=origArgs['num']
else
if (origArgs['number'] and origArgs['number'] ~= '') then
num=origArgs['number']
else
if (origArgs['1'] and origArgs['1'] ~= '') then
num=origArgs['1']
else
err="1"
end
end
end
if (origArgs['b'] and origArgs['b'] ~= '') then
bigstr=origArgs['b']
else
if (origArgs['daiji'] and origArgs['daiji'] ~= '') then
bigstr=origArgs['daiji']
else
if (origArgs['big'] and origArgs['big'] ~= '') then
bigstr=origArgs['big']
else
bigstr=0
end
end
end
big = false
if (bigstr ~= '') then
trybig=tonumber(bigstr)
if (trybig ~= '') then
if (trybig >0) then
big = true
end
end
else
big = yesno(bigstr)
end
return p.NumberToChinese(num,big)
end
function p.getIntegerString(str)
local body ="" -- create and start the output string
strlen=string.len(str)
for i = 1,strlen do
getstr = string.sub(str,i,i)
if (string.byte(getstr) ~=46) then
body= body .. getstr
else
break
end
end
return body
end
function p.NumberListToChinese(num,bigw)
local body ='' -- create and start the output string
nlen=string.len(num)
for i = 1,nlen do
body = body .. p.getChineseNumber(tonumber(string.sub(num,i,i)),bigw)
end
return body
end
function p.NumberToChinese(num,bigw)
local body ='' -- create and start the output string
if ((num == "∞") or (num == "+∞")) then
return "正無窮大"
elseif (num == "-∞") then
return "負無窮大"
elseif (num == "0") then
return p.getChineseNumber(tonumber(0),bigw0)
elseif (num == p.getIntegerString(num)) then
return p.IntegerToChinese(num,bigw)
end
getedint = p.getIntegerString(num)
getedfact = string.sub(num,string.len(getedint)+2,string.len(num))
if (tonumber(getedint) == 0) then
if (tonumber(getedfact) == 0) then
body = p.getChineseNumber(tonumber(0),bigw0)
else
body = p.getChineseNumber(tonumber(0),bigw0) .. "點" .. p.NumberListToChinese(getedfact,bigw)
end
else
body = body .. p.IntegerToChinese(getedint,bigw) .. "點" .. p.NumberListToChinese(getedfact,bigw)
end
return body
end
function p.IntegerToChinese(num,bigw)
-- For calling from other Lua modules.
local body ='' -- create and start the output string
nlen=string.len(num)
alllen=math.ceil(nlen/4)
lastz=0
tnum = {}
ntnum = {}
for i = alllen-1,0, -1 do
ij = i+1
tnum[ij+1]=''
if (nlen-i*4-3 <= 0) then
sub0=0
else
sub0=nlen-i*4-3
end
if (sub0-4 <= 0) then
lastz=1
else
lastz=tonumber(string.sub(num,sub0-4,sub0))
end
geted=string.sub(num,sub0,nlen-i*4)
trned = p.CChineseSide(geted,bigw,lastz)
body=body .. trned .. main[ij]
end
if (tonumber(num)<20 and tonumber(num)>=10) then
if (bigw) then
body="拾"
else
body="十"
end
if (tonumber(num)>10) then
body=body..p.getChineseNumber(tonumber(num)-10,bigw)
else
end
end
if (tonumber(num)<10) then
body=p.getChineseNumber(tonumber(num),bigw)
end
return body -- return result
end
function p.CChineseSide(num,bigw0,lestn)
lestn1=lestn
numlen=string.len(num)
trans=''
for i = 0,numlen-1 do
j=numlen-i
k=i+1
numid = tonumber(string.sub(num,k,k))
if (bigw0) then
if(((tonumber(num)>=10) and (tonumber(num)<20)) and (j == 1)) then
trans=trans .. p.getChineseZero(lastn1,numid,bigw0," ")
else
trans=trans .. p.getChineseZero(lastn1,numid,bigw0,deputybig[j])
end
else
trans=trans .. p.getChineseZero(lastn1,numid,bigw0,deputy[j])
end
lastn1 = numid
end
return trans
end
function p.getChineseNumber(numid,bigw0)
--轉換小於十的數字
trans=''
if (bigw0) then
trans=numberbig[numid+1]
else
trans=numberch[numid+1]
end
return trans
end
function p.getChineseZero(lastn,num,bigw0,scale)
--處理'零'的部分
trans=''
if (lastn==0) then
if (tonumber(num)==0) then
trans=''
else
trans=p.getChineseNumber(tonumber(0),bigw0)..p.getChineseNumber(tonumber(num),bigw0)..scale
end
else
if (tonumber(num)==0) then
trans=''
else
trans=p.getChineseNumber(tonumber(num),bigw0)..scale
end
end
return trans
end
return p