模組: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)
local args = require('Module:Arguments').getArgs(frame)
local num = args.num or args.number or args[1] or 0
local big = args.b or args.daiji or 0
big = tonumber(big) or 0
if (big > 0) then
big = 1
end
big = yesno(big)
return p.NumberToChinese(num, big)
end
function p.Number_to____(frame)
local args = require('Module:Arguments').getArgs(frame)
local num = args.num or args.number or args[1] or 0
local big = args.b or args.daiji or 0
big = tonumber(big) or 0
if (big > 0) then
big = 1
end
big = yesno(big)
return p.NumberTo___(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.NumberTo___(num, bigw)
local body = '' -- create and start the output string
nlen = string.len(num)
for i = 1, nlen do
testnum = tonumber(string.sub(num, i, i))
if(testnum == 0)then
body = body .. "〇"
else
body = body .. p.getChineseNumber(tonumber(string.sub(num, i, i)), bigw)
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=''
if (tonumber(num)<20 and tonumber(num)>=10) then
if (bigw0) then
trans="拾"
else
trans="十"
end
if (tonumber(num)>10) then
if (bigw0) then
trans=trans .. p.getChineseZero(lastn1,tonumber(num)-10,bigw0,"")
else
trans=trans .. p.getChineseZero(lastn1,tonumber(num)-10,bigw0,"")
end
else
end
return trans
end
for i = 0,numlen-1 do
j=numlen-i
k=i+1
numid = tonumber(string.sub(num,k,k))
if (bigw0) then
trans=trans .. p.getChineseZero(lastn1,numid,bigw0,deputybig[j])
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