模組: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 main = {"","萬","億","兆","京","垓","秭","穰","溝","澗","正","載","極","恆河沙","阿僧祇","那由他","不可思議","無量大數"}
local numberch = {"零","一","二","三","四","五","六","七","八","九"}
local numberbig = {"零","壹","貳","參","肆","伍","陸","柒","捌","玖"}
local deputybig = {"","拾","佰","仟"}
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
if (num ~= '') then
if (tonumber(num) ~= '') then
thenumber = num
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.CChinese(thenumber,big)
end
function p.CChinese(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)
tnum = {}
ntnum = {}
for i = 0,alllen-1 do
ij = i+1
tnum[ij+1]=''
if (nlen-i*4-3 <= 0) then
sub0=0
else
sub0=nlen-i*4-3
end
geted=string.sub(num,sub0,nlen-i*4)
trned = p.CChineseSide(geted,bigw)
if (string.sub(geted,string.len(geted),string.len(geted))=='0') then
if (bigw) then
tnum[ij+1]=tnum[ij+1]..numberbig[numid]
else
tnum[ij+1]=tnum[ij+1]..numberch[numid]
end
end
reget=''
for jk = 1,string.len(trned) do
if (bigw) then
if (string.sub(trned,i,i) == numberbig[1]) then
if (string.sub(trned,i+1,i+1) == numberbig[1]) then
else
reget=reget..string.sub(trned,i,i)
end
end
else
if (string.sub(trned,i,i) == numberch[1]) then
if (string.sub(trned,i+1,i+1) == numberch[1]) then
else
reget=reget..string.sub(trned,i,i)
end
end
end
end
reget=reget..main[ij]
body=body..reget
--table.insert(ntnum,string.sub(num,sub0,nlen-i*4))
end
mw.log(body)
return body -- return result
end
function p.CChineseSide(num,bigw0)
trans=''
for i = 0,3 do
j=4-i
k=i+1
numid = tonumber(string.sub(num,k,k))+1
if (numid == 1) then
if (bigw0) then
trans=trans .. numberbig[numid]
else
trans=trans .. numberch[numid]
end
else
if (bigw0) then
trans=trans .. numberbig[numid] .. deputybig[j]
else
trans=trans .. numberch[numid] .. deputy[j]
end
end
end
mw.log(trans)
return trans
end
return p