Jump to content

Module:ConvertNumeric

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dcoetzee (talk | contribs) at 02:27, 24 February 2013 (Create with first draft, tested offline). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- Module for converting between different representations of numbers.

local ones_position = {
    [0] = 'zero',
    [1] = 'one',
    [2] = 'two',
    [3] = 'three',
    [4] = 'four',
    [5] = 'five',
    [6] = 'six',
    [7] = 'seven',
    [8] = 'eight',
    [9] = 'nine',
    [10] = 'ten',
    [11] = 'eleven',
    [12] = 'twelve',
    [13] = 'thirteen',
    [14] = 'fourteen',
    [15] = 'fifteen',
    [16] = 'sixteen',
    [17] = 'seventeen',
    [18] = 'eighteen',
    [19] = 'nineteen',
}

local tens_position = {
    [2] = 'twenty',
    [3] = 'thirty',
    [4] = 'forty',
    [5] = 'fifty',
    [6] = 'sixty',
    [7] = 'seventy',
    [8] = 'eighty',
    [9] = 'ninety'
}

function numeral_to_english_less_100(num)
    if num < 20 then
        return ones_position[num]
    elseif num % 10 == 0 then
        return tens_position[num / 10]
    else
        return tens_position[math.floor(num / 10)] .. '-' .. ones_position[num % 10]
    end
end

function numeral_to_english_less_1000(num, use_and)
    if num < 100 then
        return numeral_to_english_less_100(num)
    elseif num % 100 == 0 then
        return ones_position[num/100] .. ' hundred'
    else
        local s = ones_position[math.floor(num/100)] .. ' hundred '
        if use_and then s = s .. 'and ' end
        s = s .. numeral_to_english_less_100(num % 100)
        return s
    end
end

local p = {}

function p._numeral_to_english(num, capitalize)
    local s = ''
    if num >= 1000000000 then
        s = s .. numeral_to_english_less_1000(math.floor(num/1000000000), false) .. ' billion'
        num = num % 1000000000
    end
    if num >= 1000000 then
        if s ~= '' then s = s .. ', ' end
        s = s .. numeral_to_english_less_1000(math.floor(num/1000000), false) .. ' million'
        num = num % 1000000
    end
    if num >= 1000 then
        if s ~= '' then s = s .. ', ' end
        s = s .. numeral_to_english_less_1000(math.floor(num/1000), false) .. ' thousand'
        num = num % 1000
    end
    if s ~= '' and num > 0 then
        if num >= 100 then
            s = s .. ', '
        else
            s = s .. ' and '
        end
    end
    if s == '' or num > 0 then
        s = s .. numeral_to_english_less_1000(math.floor(num), true)
    end
    
    s = s:gsub("^%s*(.-)%s*$", "%1")   -- Trim whitespace
    if capitalize then s = s:gsub("^%l", string.upper) end
    
    return s
end

function p.numeral_to_english(frame)
    local num = tonumber(frame.args[1])
    local capitalize = frame.args['capitalize']
    return _numeral_to_english(num, capitalize)
end

return p