ماژول:Number to word

ویکی‌پدیا، آزادِ دانشنومه، جه

توضیحات این پودمان می‌تواند در ماژول:Number to word/توضیحات قرار گیرد.

local p = {}

local convert = require('Module:Numeral converter').convert

local yekan = {
    '',
    'یک',
    'دو',
    'سه',
    'چار',
    'پنج',
    'شیش',
    'هفت',
    'هشت',
    'نه'}

local dahgan = {
    '',
    '',
    'بیست',
    'سی',
    'چل',
    'پنجاه',
    'شصت',
    'هفتاد',
    'هشتاد',
    'نود'}

local sadgan = {
    '',
    'صد',
    'دٮویست',
    'سیصد',
    'چارصد',
    'پونصد',
    'شیشصد',
    'هفتصد',
    'هشتصد',
    'نهصد'}

local exceptions = {
    'ده',
    'یازه',
    'دوازه',
    'سیزه',
    'چارده',
    'پونزه',
    'شونزه',
    'هیوده',
    'هیژده',
    'نوزده'}

local scale = {
    '',
    ' هزار',
    ' میلیون ',
    ' میلیارد',
    ' بیلیون',
    ' بیلیارد',
    ' تریلیون',
    ' ترلیارد',
    ' کوآدریلیون',
    ' کادریلیارد',
    ' کوینتیلیون',
    ' کوانتینیارد'}

function threedigit_words(threedigit)
    local words = ''
    if string.sub(threedigit,1,1) ~= '0' and string.sub( threedigit, 2)~='00' then    
    	words = sadgan[tonumber(string.sub(threedigit,1,1))+1] .. ' و '
	else
		words = sadgan[tonumber(string.sub(threedigit,1,1))+1]
	end
    if string.sub(threedigit,2,2) == '1' then
        words = words .. exceptions[tonumber(string.sub(threedigit,3,3))+1]
    else
		if string.sub(threedigit,3,3) ~= '0' and string.sub(threedigit,2,2)~='0' then
			words = words .. dahgan[tonumber(string.sub(threedigit,2,2))+1] .. ' و '
		else
			words = words .. dahgan[tonumber(string.sub(threedigit,2,2))+1]
		end
		words = words .. yekan[tonumber(string.sub(threedigit,3,3))+1]
	end
    return words
end

function strnum2words(strnumber)
    if tonumber(strnumber) == 0 then
        return 'صفر'
	end
    if #strnumber > #scale * 3 then
        error('خارج از محدوده!')
    end
	
    length = #strnumber
    
    if length%3 ~= 0 then
        strnumber = string.rep('0', 3-length%3) .. strnumber
    end
	
    groups = (#strnumber) / 3
    local words = ''
    group = groups
    while group > 0 do
        threedigit = string.sub( strnumber, group*3-2, group*3)
        word3 = threedigit_words(threedigit)
        if word3 ~= '' and group ~= groups then
            if words == '' then
                words = word3 .. scale[groups-group+1]
            else
                words = word3 .. scale[groups-group+1] .. ' و ' .. words
			end
        else
            words = word3 .. words
		end
        group = group - 1
	end
	
    return sign.. words
end

function ordinal(nubmer)
    words = strnum2words(nubmer)
    if mw.ustring.sub(words,-2) == 'سه' then
        words = words:gsub('سه$', 'سوم')
        return words
    else
        return words .. 'م'
	end
end

function ordinal2(nubmer)
    words = strnum2words(nubmer)
    if mw.ustring.sub(words,-2) == 'سه' then
        words = words:gsub('سه$', 'سومین')
        return words
    else
        return words .. 'مین'
	end
end

function p.run(frame)
    local args = frame.args
	number = mw.ustring.match(convert("en",args[1]), "(%d*)%W*$")
	if string.sub(number,1,1) == '-' or string.sub(number,1,1) == '−' then
		sign = 'منفی '
		number = mw.text.trim(string.sub(number,2))
	else
		sign = ''
	end
	if args[2] == 'م' then
		return ordinal(number)
	elseif args[2] == 'مین' then
		return ordinal2(number)
	else
		return strnum2words(number)
	end
end
 
return p