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 05:46, 24 February 2013 (Implement ordinals, fix regression with e.g. '1000'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- 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 ones_position_ord = {
    [0] = 'zeroth',
    [1] = 'first',
    [2] = 'second',
    [3] = 'third',
    [4] = 'fourth',
    [5] = 'fifth',
    [6] = 'sixth',
    [7] = 'seventh',
    [8] = 'eighth',
    [9] = 'ninth',
    [10] = 'tenth',
    [11] = 'eleventh',
    [12] = 'twelfth',
    [13] = 'thirteenth',
    [14] = 'fourteenth',
    [15] = 'fifteenth',
    [16] = 'sixteenth',
    [17] = 'seventeenth',
    [18] = 'eighteenth',
    [19] = 'nineteenth'
}

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

local tens_position_ord = {
    [2] = 'twentieth',
    [3] = 'thirtieth',
    [4] = 'fortieth',
    [5] = 'fiftieth',
    [6] = 'sixtieth',
    [7] = 'seventieth',
    [8] = 'eightieth',
    [9] = 'ninetieth'
}

local groups = {
    [1] = 'thousand',
    [2] = 'million',
    [3] = 'billion',
    [4] = 'trillion',
    [5] = 'quadrillion',
    [6] = 'quintillion',
    [7] = 'sextillion',
    [8] = 'septillion',
    [9] = 'octillion',
    [10] = 'nonillion',
    [11] = 'decillion',
    [12] = 'undecillion',
    [13] = 'duodecillion',
    [14] = 'tredecillion',
    [15] = 'quattuordecillion',
    [16] = 'quindecillion',
    [17] = 'sexdecillion',
    [18] = 'septendecillion',
    [19] = 'octodecillion',
    [20] = 'novemdecillion',
    [21] = 'vigintillion',
}

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

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

function _numeral_to_english(num, capitalize, omit_and, hyphenate, ordinal)
    num = num:gsub(",", "")   -- Remove commas
    local negative = num:find("^%-")
    local decimal_places, subs = num:gsub("^%-?%d*%.(%d+)$", "%1")
    if subs == 0 then decimal_places = nil end
    num, subs = num:gsub("^%-?(%d*)%.?%d*$", "%1")
    if num == '' and decimal_places then num = '0' end
    if subs == 0 or num == '' then return 'Invalid decimal numeral' end
    
    local s = ''
    while #num > 3 do
        if s ~= '' then s = s .. ', ' end
        local group_num = math.floor((#num - 1) / 3)
        local group = groups[group_num]
        local group_digits = #num - group_num*3
        s = s .. numeral_to_english_less_1000(num:sub(1, group_digits), false, false) .. ' ' .. group
        num = num:sub(1 + group_digits)
        num = num:gsub("^0*", "")   -- Trim leading zeros
    end
    if s ~= '' and num ~= '' then
        if #num >= 2 then
            s = s .. ', '
        elseif omit_and then
            s = s .. ' '
        else
            s = s .. ' and '
        end
    end
    if s == '' or num ~= '' then
        s = s .. numeral_to_english_less_1000(num, not omit_and, ordinal)
    elseif ordinal then
        s = s .. 'th'
    end
    
    if decimal_places then
        s = s .. ' point'
        for i = 1, #decimal_places do
            s = s .. ' ' .. ones_position[tonumber(decimal_places:sub(i,i))]
        end
    end
    
    s = s:gsub("^%s*(.-)%s*$", "%1")   -- Trim whitespace
    if negative then s = 'negative ' .. s end
    if hyphenate then s = s:gsub("%s", "-") end
    if capitalize then s = s:gsub("^%l", string.upper) end
    
    return s
end

local p = {}

function p.numeral_to_english(frame)
    local num = frame.args[1]
    num = num:gsub("^%s*(.-)%s*$", "%1")   -- Trim whitespace
    local case = frame.args['case']
    local sp = frame.args['sp']
    local adj = frame.args['adj']
    local ord = frame.args['ord']
    return _numeral_to_english(num, case == 'U' or case == 'u', sp == 'us', adj == 'on', ord == 'on')
end

return p