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 12:38, 24 February 2013 (If it doesn't fit the format, make it evaluate the arg as an expression). 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 ones_position_plural = {
    [0] = 'zeros',
    [1] = 'ones',
    [2] = 'twos',
    [3] = 'threes',
    [4] = 'fours',
    [5] = 'fives',
    [6] = 'sixes',
    [7] = 'sevens',
    [8] = 'eights',
    [9] = 'nines',
    [10] = 'tens',
    [11] = 'elevens',
    [12] = 'twelves',
    [13] = 'thirteens',
    [14] = 'fourteens',
    [15] = 'fifteens',
    [16] = 'sixteens',
    [17] = 'seventeens',
    [18] = 'eighteens',
    [19] = 'nineteens'
}

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 tens_position_plural = {
    [2] = 'twenties',
    [3] = 'thirties',
    [4] = 'forties',
    [5] = 'fifties',
    [6] = 'sixties',
    [7] = 'seventies',
    [8] = 'eighties',
    [9] = 'nineties'
}

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, plural)
    local terminal_ones, terminal_tens
    if ordinal then
        terminal_ones = ones_position_ord
        terminal_tens = tens_position_ord
    elseif plural then
        terminal_ones = ones_position_plural
        terminal_tens = tens_position_plural
    else
        terminal_ones = ones_position
        terminal_tens = tens_position
    end

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

function standard_suffix(ordinal, plural)
    if ordinal then return 'th' end
    if plural then return 's' end
    return ''
end

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

function scientific_notation_to_decimal(num)
    local exponent, subs = num:gsub("^%-?%d*%.?%d*%-?[Ee]([+%-]?%d+)$", "%1")
    if subs == 0 then return num end
    exponent = tonumber(exponent)

    local negative = num:find("^%-")
    _, decimal_pos = num:find("%.")
    local mantissa = num:gsub("^%-?(%d*)%.?(%d*)%-?[Ee][+%-]?%d+$", "%1%2")
    if negative and decimal_pos then decimal_pos = decimal_pos - 1 end
    if not decimal_pos then decimal_pos = #mantissa + 1 end
    local prev_len = #num
    
    while decimal_pos > 1 and mantissa:sub(1,1) == '0' do
        mantissa = mantissa:sub(2)
        decimal_pos = decimal_pos - 1
    end
    while exponent > 0 do
        decimal_pos = decimal_pos + 1
        exponent = exponent - 1
        if decimal_pos > #mantissa + 1 then mantissa = mantissa .. '0' end
        while decimal_pos > 1 and mantissa:sub(1,1) == '0' do
            mantissa = mantissa:sub(2)
            decimal_pos = decimal_pos - 1
        end
    end
    while exponent < 0 do
        if decimal_pos == 1 then
            mantissa = '0' .. mantissa
        else
            decimal_pos = decimal_pos - 1
        end
        exponent = exponent + 1
    end
    
    return (negative and '-' or '') .. mantissa:sub(1, decimal_pos - 1) .. '.' .. mantissa:sub(decimal_pos)
end

function round_num(x)
    if x%1 >= 0.5 then
        return math.ceil(x)
    else 
        return math.floor(x)
    end
end

function round_for_english(num, round)
    if num:find("^%-?%d?%d%.?$") or num:find("^%-?%d?%d%.%d*$") then
        -- Two-digit numbers will fit in Lua numbers
        num = tonumber(num)
        if round == 'on' then return tostring(round_num(num)) end
        if round == 'up' then return tostring(math.ceil(num)) end
        if round == 'down' then return tostring(math.floor(num)) end
        return nil
    end

    local negative = num:find("^%-")
    if negative then
        -- We're rounding magnitude so flip it
        if round == 'up' then round = 'down' end
        if round == 'down' then round = 'up' end
    end
    nonzero_digits = 0
    for digit in num:gfind("[1-9]") do
        nonzero_digits = nonzero_digits + 1
    end
    num = num:gsub("%.%d*$", "") -- Remove decimal part
    -- Result based on first two digits, rest turn to zero
    _, _, lead_digit, round_digit, rest = num:find("^%-?(%d)(%d)(%d*)$")
    if (round == 'up' and nonzero_digits > 1) or (round == 'on' and tonumber(round_digit) >= 5) then
        lead_digit = tostring(tonumber(lead_digit) + 1)
    end
    rest = rest:gsub("%d", "0")
    return (negative and '-' or '') .. lead_digit .. '0' .. rest
end

function _numeral_to_english(num, capitalize, use_and, hyphenate, ordinal, plural, links, negative_word, round)
    num = scientific_notation_to_decimal(num)

    if round and round ~= 'on' and round ~= 'up' and round ~= 'down' then return 'Invalid rounding mode' end
    if round then num = round_for_english(num, round) end

    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, false) .. ' '
        if ((links == 'on' and group_num >= 3) or links:find(group)) and group_num <= 13 then
            s = s .. '[[Orders_of_magnitude_(numbers)#10' .. group_num*3 .. '|' .. group .. ']]'
        else
            s = s .. group
        end
        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, use_and, ordinal, plural)
    elseif ordinal or plural then
        s = s .. standard_suffix(ordinal, plural)
    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 ordinal and plural then s = s .. 's' end  -- s suffix works for all ordinals
    if negative and s ~= 'zero' then s = negative_word .. ' ' .. 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
    num = num:gsub(",", "")   -- Remove commas
    if not num:find("^%-?%d*%.?%d*%-?[Ee]?[+%-]?%d*$") then
        num = frame:preprocess('{{#expr: ' .. num .. '}}')
    end

    local case = frame.args['case']
    local sp = frame.args['sp']
    local adj = frame.args['adj']
    local ord = frame.args['ord']
    local pl = frame.args['pl']
    local lk = frame.args['lk'] or ''
    local negative = frame.args['negative'] or 'negative'
    local round = frame.args['round'] or nil    
    return _numeral_to_english(num, case == 'U' or case == 'u', sp ~= 'us', adj == 'on', ord == 'on', pl == 'on', lk, negative, round)
end

return p