มอดูล:Thai number
หน้าตา
นี่เป็นมอดูลที่ถูกป้องกันถาวรเนื่องจากมีความเสี่ยงสูง กรุณาอภิปรายการเปลี่ยนแปลงใด ๆ ทางหน้าคุย คุณอาจส่งคำขอแก้ไขไปยังผู้ดูแลระบบเพื่อให้แก้ไขได้หากเป็นการแก้ไขที่ไม่มีการคัดค้านหรือมีความเห็นพ้องสนับสนุน คุณยังสามารถขอให้เลิกป้องกันหน้าได้ |
คู่มือการใช้งานมอดูล[สร้าง]
คุณอาจจะต้องการสร้างคู่มือการใช้งานของมอดูลนี้ ผู้เขียนสามารถทำการทดลองได้ที่กระบะทราย (แก้ | ดูความแตกต่าง) และชุดทดสอบ (แก้) ของมอดูลนี้ โปรดเพิ่มหมวดหมู่ไปที่หน้าย่อย /doc หน้าย่อยของมอดูลนี้ |
local p = {}
local data = {
['normal'] = {
[0] = '',
[1] = 'หนึ่ง',
[2] = 'สอง',
[3] = 'สาม',
[4] = 'สี่',
[5] = 'ห้า',
[6] = 'หก',
[7] = 'เจ็ด',
[8] = 'แปด',
[9] = 'เก้า',
},
[0] = {
[0] = '',
[1] = 'เอ็ด',
[2] = 'สอง',
[3] = 'สาม',
[4] = 'สี่',
[5] = 'ห้า',
[6] = 'หก',
[7] = 'เจ็ด',
[8] = 'แปด',
[9] = 'เก้า',
['value'] = '',
['mode'] = 1,
},
[1] = {
[0] = '',
[1] = ' ',
[2] = 'ยี่',
[3] = 'สาม',
[4] = 'สี่',
[5] = 'ห้า',
[6] = 'หก',
[7] = 'เจ็ด',
[8] = 'แปด',
[9] = 'เก้า',
['value'] = 'สิบ',
['mode'] = 1,
},
[2] = {
['value'] = 'ร้อย',
['mode'] = 0,
},
[3] = {
['value'] = 'พัน',
['mode'] = 0,
},
[4] = {
['value'] = 'หมื่น',
['mode'] = 0,
},
[5] = {
['value'] = 'แสน',
['mode'] = 0,
},
[6] = {
[0] = '',
[1] = 'เอ็ด',
[2] = 'สอง',
[3] = 'สาม',
[4] = 'สี่',
[5] = 'ห้า',
[6] = 'หก',
[7] = 'เจ็ด',
[8] = 'แปด',
[9] = 'เก้า',
['value'] = 'ล้าน',
['mode'] = 1,
},
}
local function _toWord(num)
number = tonumber(num)
if not number or number < 0 or number == math.huge then
return
end
if num == '0' then
return 'ศูนย์'
end
local level = 0
result = {}
for i = #num, 1, -1 do
table.insert(result, data[level]['value'])
if data[level]['mode'] == 0 then
table.insert(result, data['normal'][tonumber(num:sub(i, i))])
else
table.insert(result, data[level][tonumber(num:sub(i, i))])
end
level = level + 1
if level > 6 then
level = 1
end
end
for i = 1, #result do
if result[i] == 'เอ็ด' and result[i + 2] == nil then
result[i] = 'หนึ่ง'
end
if i % 2 == 0 and result[i] == '' and (i > 2 and (i - 2) % 12 ~= 0) then
result[i - 1] = ''
end
if result[i] == ' ' then
result[i] = ''
end
end
newresult = {}
for i = #result, 1, -1 do
table.insert(newresult, result[i])
end
return table.concat(newresult)
end
local function _toNumber(num)
number = tonumber(num)
if not number or number < 0 or number == math.huge then
return
end
num, _cnt = num:gsub('0', '๐')
:gsub('1', '๑')
:gsub('2', '๒')
:gsub('3', '๓')
:gsub('4', '๔')
:gsub('5', '๕')
:gsub('6', '๖')
:gsub('7', '๗')
:gsub('8', '๘')
:gsub('9', '๙')
return num
end
local function parseInput(frame)
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
for k, v in pairs( frame.args ) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Trim whitespace and remove blank arguments.
local args = {}
for k, v in pairs( origArgs ) do
if type( v ) == 'string' then
v = mw.text.trim( v )
end
if v ~= '' then
args[k] = v
end
end
return args
end
function p.toNumber( frame )
-- If called via #invoke, use the args passed into the invoking
-- template, or the args passed to #invoke if any exist. Otherwise
-- assume args are being passed directly in from the debug console
-- or from another Lua module.
args = parseInput(frame)
return _toNumber(args[1])
end
function p.toWord( frame )
-- If called via #invoke, use the args passed into the invoking
-- template, or the args passed to #invoke if any exist. Otherwise
-- assume args are being passed directly in from the debug console
-- or from another Lua module.
args = parseInput(frame)
return _toWord(args[1])
end
return p