-- This module converts a number into its written English form.
-- For example, "2" becomes "two", and "79" becomes "seventy-nine".
local getArgs = require('Module:Arguments').getArgs
local numConv = require('Module:Numeral converter').convert
local p = {}
local max = 100 -- The maximum number that can be parsed.
local ones = {
[0] = 'صفر',
[1] = 'یک',
[2] = 'دو',
[3] = 'سه',
[4] = 'چهار',
[5] = 'پنج',
[6] = 'شش',
[7] = 'هفت',
[8] = 'هشت',
[9] = 'نه'
}
local specials = {
[10] = 'ده',
[11] = 'یازده',
[12] = 'دوازده',
[13] = 'سیزده',
[15] = 'پانزده',
[16] = 'شانزده',
[17] = 'هفده',
[18] = 'هجده',
[19] = 'نوزده',
[20] = 'بیست',
[30] = 'سی',
[40] = 'چهل',
[50] = 'پنجاه',
[60] = 'شصت',
[70] = 'هفتاد',
[80] = 'هشتاد',
[90] = 'نود',
[100] = 'صد'
}
local formatRules = {
{num = 90, rule = 'نود و %s'},
{num = 80, rule = 'هشتاد و %s'},
{num = 70, rule = 'هفتاد و %s'},
{num = 60, rule = 'شصت و %s'},
{num = 50, rule = 'پنجاه و %s'},
{num = 40, rule = 'چهل و %s'},
{num = 30, rule = 'سی و %s'},
{num = 20, rule = 'بیست و %s'},
{num = 10, rule = '%sده'}
}
function p.main(frame)
local args = getArgs(frame)
local num = tonumber(numConv('en', args[1]))
local success, result = pcall(p._main, num)
if success then
return result
else
return mw.ustring.format('<strong class="error">خطا: %s</strong>', result) -- "result" is the error message.
end
return p._main(num)
end
function p._main(num)
if type(num) ~= 'number' or math.floor(num) ~= num or num < 0 or num > max then
error('ورودی باید یک عدد بین ۰ و ' .. numConv('fa', tostring(max)) .. ' باشد', 2)
end
-- Check for numbers from 0 to 9.
local onesVal = ones[num]
if onesVal then
return onesVal
end
-- Check for special numbers.
local specialVal = specials[num]
if specialVal then
return specialVal
end
-- Construct the number from its format rule.
onesVal = ones[num % 10]
if not onesVal then
error('خطای دور از انتظار در زمان تجزیهٔ ورودی ' .. numConv('fa', tostring(num)))
end
for i, t in ipairs(formatRules) do
if num >= t.num then
return mw.ustring.format(t.rule, onesVal)
end
end
error('قاعدهٔ قالببندی برای ورودی ' .. numConv('fa', tostring(num)) .. ' یافت نشد')
end
return p