Módulo:ConvertNumeric
Uso
{{#invoke:ConvertNumeric|nombre_de_la_función}}
La función principal es numeral_to_spanish
y recibe los siguientes parámetros opcionales a parte del número:
case=u o case=U
: para mostrar la primera letra en mayúsculaord=on
: para devolver el número deletreado en ordinallk=on o lk=grupo
: para generar enlaces a las magnitudesnegative=palabra
utilizaría "palabra" en vez de "menos" con números negativos- Ejemplo: con -1
negative=-
generaría "- uno"negative=Queen
generaría "Queen uno"- por defecto se generaría "menos uno"
- Ejemplo: con -1
También están disponibles las siguientes funciones:
decToHex
: para convertir de números decimales a hexadecimales- Ejemplo: 50 -> 32
_roman_to_numeral
: para convertir un número romano a decimal- Ejemplo: VI -> 6
Colaborar
Las pruebas del módulo se pueden encontrar en Módulo:ConvertNumeric/tests y el resultado de su ejecución en Módulo discusión:ConvertNumeric/tests
Antes de modificar el módulo prueba tus cambios en Módulo:ConvertNumeric/sandbox y observa el resultado de aplicar los tests en Módulo discusión:ConvertNumeric/sandbox/testcases
-- Módulo para convertir entre diferentes representaciones de números -- Los tests están en [[Módulo:ConvertNumeric/tests]], úsalos para verificar las ediciones -- Antes de editar el módulo prueba tus cambios en [[Módulo:ConvertNumeric/sandbox]] y verifica que funcionan en [[Módulo_discusión:ConvertNumeric/sandbox/testcases]] local posicion_unidades = { [0] = 'cero', [1] = 'uno', [2] = 'dos', [3] = 'tres', [4] = 'cuatro', [5] = 'cinco', [6] = 'seis', [7] = 'siete', [8] = 'ocho', [9] = 'nueve', [10] = 'diez', [11] = 'once', [12] = 'doce', [13] = 'trece', [14] = 'catorce', [15] = 'quince', [16] = 'dieciseis', [17] = 'diecisiete', [18] = 'dieciocho', [19] = 'diecinueve' } local posicion_unidades_ord = { [0] = 'cero', -- No existe en español una diferencia [1] = 'primero', [2] = 'segundo', [3] = 'tercero', [4] = 'cuarto', [5] = 'quinto', [6] = 'sexto', [7] = 'séptimo', [8] = 'octavo', [9] = 'noveno', [10] = 'décimo', [11] = 'décimo primero', [12] = 'duodécimo', [13] = 'décimo tercero', [14] = 'décimo cuarto', [15] = 'décimo quinto', [16] = 'décimo sexto', [17] = 'décimo séptimo', [18] = 'décimo octavo', [19] = 'décimo noveno' } local separadores = { [0] = ' y ', -- Para cardinales [1] = ' ', -- Para ordinales } local posicion_decenas = { [2] = 'veinte', [3] = 'treinta', [4] = 'cuarenta', [5] = 'cincuenta', [6] = 'sesenta', [7] = 'setenta', [8] = 'ochenta', [9] = 'noventa' } local posicion_decenas_ord = { [2] = 'vigésimo', [3] = 'trigésimo', [4] = 'cuadragésimo', [5] = 'quincuagésimo', [6] = 'sexagésimo', [7] = 'septuagésimo', [8] = 'octogésimo', [9] = 'nonagésimo' } local posicion_centenas = { [1] = 'cien', [2] = 'doscientos', [3] = 'trescientos', [4] = 'cuatrocientos', [5] = 'quinientos', [6] = 'seiscientos', [7] = 'setecientos', [8] = 'ochocientos', [9] = 'novecientos' } local posicion_centenas_ord = { [1] = 'centésimo', [2] = 'ducentésimo', [3] = 'tricentésimo', [4] = 'cuadringentésimo', [5] = 'quingentésimo', [6] = 'sexcentésimo', [7] = 'septingentésimo', [8] = 'octingentésimo', [9] = 'noningentésimo' } local groups = { [1] = 'mi', [2] = 'bi', [3] = 'tri', [4] = 'cuatri', [5] = 'quinti', [6] = 'sexti', [7] = 'septi', [8] = 'octi', [9] = 'noni', [10] = 'deci', [11] = 'undeci', [12] = 'duodeci', [13] = 'tredeci', [14] = 'cuatordeci', [15] = 'quindeci', [16] = 'sexdeci', [17] = 'septendeci', [18] = 'octodeci', [19] = 'novendeci', [20] = 'viginti' } local decimal_small_groups = { [1] = 'décima', [2] = 'centésima', [3] = 'milésima', [4] = 'diezmilésima', [5] = 'cienmilésima', } local roman_numerals = { I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 } -- Converts a given valid roman numeral (and some invalid roman numerals) to a number. Returns -1, errorstring on error local function roman_to_numeral(roman) if type(roman) ~= "string" then return -1, "el número romano no es un string" end local rev = roman:reverse() local raising = true local last = 0 local result = 0 for i = 1, #rev do local c = rev:sub(i, i) local next = roman_numerals[c] if next == nil then return -1, "el número romano contiene un caracter erróneo " .. c end if next > last then result = result + next raising = true elseif next < last then result = result - next raising = false elseif raising then result = result + next else result = result - next end last = next end return result end -- Convierte un número entre 0 y 100 a texto en Español (ejemplo 47 -> cuarenta y siete) local function numeral_to_spanish_less_100(num, ordinal, zero, final) local unidades, decenas if ordinal then unidades = posicion_unidades_ord decenas = posicion_decenas_ord sep = separadores[1] else unidades = posicion_unidades decenas = posicion_decenas sep = separadores[0] end local value = '' if num == 0 and zero ~= nil then -- Si es 0 y se especifica valor para el caso cero value = zero elseif num < 20 then -- Caso para números menores de 20 que son raros en general value = unidades[num] elseif num % 10 == 0 then -- Caso genérico para veinte, treinta, cuarenta etc | cuadragésimo value = decenas[num / 10] elseif math.floor(num / 10) == 2 and not ordinal then -- Caso especial para el veintiuno veintidos veintitres etc.... local tmp = 'veinti' .. unidades[num % 10] if tmp == 'veintitres' then tmp = 'veintitrés' end value = tmp else -- Caso genérico de noventa y uno, cincuenta y dos | vigesimo tercero value = decenas[math.floor(num / 10)] .. sep .. unidades[num % 10] end if not final and value == 'cero' then value = '' end return value end -- Convierte un entero entre 0 y 1000 a texto en Español (e.g. 475 -> cuatrocientos setenta y cinco) local function numeral_to_spanish_less_1000(num, ordinal, zero, final) local centenas if ordinal then centenas = posicion_centenas_ord else centenas = posicion_centenas end num = tonumber(num) if num < 100 then return numeral_to_spanish_less_100(num, ordinal, zero, final) elseif num % 100 == 0 then return centenas[num/100] elseif math.floor(num/100) == 1 and not ordinal then return 'ciento' .. ' ' .. numeral_to_spanish_less_100(num % 100, ordinal, zero) else return centenas[math.floor(num/100)] .. ' ' .. numeral_to_spanish_less_100(num % 100, ordinal, zero, final) end end -- Convierte un número expresado en notación científica a la notación decimal estándar -- ejemplo 1.23E5 -> 123000, 1.23E-5 = .0000123. La conversión es exacta, sin redondeos. local function scientific_notation_to_decimal(num) local exponent, subs = num:gsub("^%-?%d*%.?%d*%-?[Ee]([+%-]?%d+)$", "%1") if subs == 0 then return num end -- Input not in scientific notation, just return unmodified exponent = tonumber(exponent) local negative = num:find("^%-") local _, decimal_pos = num:find("%.") -- Mantissa will consist of all decimal digits with no decimal point 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 -- Remove leading zeros unless decimal point is in first position while decimal_pos > 1 and mantissa:sub(1,1) == '0' do mantissa = mantissa:sub(2) decimal_pos = decimal_pos - 1 end -- Shift decimal point right for exponent > 0 while exponent > 0 do decimal_pos = decimal_pos + 1 exponent = exponent - 1 if decimal_pos > #mantissa + 1 then mantissa = mantissa .. '0' end -- Remove leading zeros unless decimal point is in first position while decimal_pos > 1 and mantissa:sub(1,1) == '0' do mantissa = mantissa:sub(2) decimal_pos = decimal_pos - 1 end end -- Shift decimal point left for exponent < 0 while exponent < 0 do if decimal_pos == 1 then mantissa = '0' .. mantissa else decimal_pos = decimal_pos - 1 end exponent = exponent + 1 end -- Insert decimal point in correct position and return return (negative and '-' or '') .. mantissa:sub(1, decimal_pos - 1) .. '.' .. mantissa:sub(decimal_pos) end -- Rounds a number to the nearest two-word number (round = up, down, or "on" for round to nearest) -- Numbers with two digits before the decimal will be rounded to an integer as specified by round. -- Larger numbers will be rounded to a number with only one nonzero digit in front and all other digits zero. -- Negative sign is preserved and does not count towards word limit. local function round_for_english(num, round) -- If an integer with at most two digits, just return if num:find("^%-?%d?%d%.?$") then return num end local negative = num:find("^%-") if negative then -- We're rounding magnitude so flip it if round == 'up' then round = 'down' elseif round == 'down' then round = 'up' end end -- If at most two digits before decimal, round to integer and return local _, _, small_int, trailing_digits, round_digit = num:find("^%-?(%d?%d?)%.((%d)%d*)$") if small_int then if small_int == '' then small_int = '0' end if (round == 'up' and trailing_digits:find('[1-9]')) or (round == 'on' and tonumber(round_digit) >= 5) then small_int = tostring(tonumber(small_int) + 1) end return (negative and '-' or '') .. small_int end -- When rounding up, any number with > 1 nonzero digit will round up (e.g. 1000000.001 rounds up to 2000000) local nonzero_digits = 0 for digit in num:gfind("[1-9]") do nonzero_digits = nonzero_digits + 1 end num = num:gsub("%.%d*$", "") -- Remove decimal part -- Second digit used to determine which way to round lead digit local _, _, lead_digit, round_digit, round_digit_2, rest = num:find("^%-?(%d)(%d)(%d)(%d*)$") if tonumber(lead_digit .. round_digit) < 20 and (1 + #rest) % 3 == 0 then -- In English numbers < 20 are one word so put 2 digits in lead and round based on 3rd lead_digit = lead_digit .. round_digit round_digit = round_digit_2 else rest = round_digit_2 .. rest end if (round == 'up' and nonzero_digits > 1) or (round == 'on' and tonumber(round_digit) >= 5) then lead_digit = tostring(tonumber(lead_digit) + 1) end -- All digits but lead digit will turn to zero rest = rest:gsub("%d", "0") return (negative and '-' or '') .. lead_digit .. '0' .. rest end -- Takes a decimal number and converts it to English text. -- Return nil if a fraction cannot be converted (only some numbers are supported for fractions). -- num (string or nil): the number to convert. -- Can be an arbitrarily large decimal, such as "-123456789123456789.345", and -- can use scientific notation (e.g. "1.23E5"). -- May fail for very large numbers not listed in "groups" such as "1E4000". -- num is nil if there is no whole number before a fraction. -- numerator (string or nil): numerator of fraction (nil if no fraction) -- denominator (string or nil): denominator of fraction (nil if no fraction) -- capitalize (boolean): whether to capitalize the result (e.g. 'One' instead of 'one') -- use_and (boolean): whether to use the word 'and' between tens/ones place and higher places -- hyphenate (boolean): whether to hyphenate all words in the result, useful for use as an adjective -- ordinal (boolean): whether to produce an ordinal (e.g. 'first' instead of 'one') -- plural (boolean): whether to pluralize the resulting number -- links: nil: do not add any links; 'on': link "billion" and larger to Orders of magnitude article; -- any other text: list of numbers to link (e.g. "billion,quadrillion") -- negative_word: word to use for negative sign (typically 'negative' or 'minus'; nil to use default) -- round: nil or '': no rounding; 'on': round to nearest two-word number; 'up'/'down': round up/down to two-word number -- zero: word to use for value '0' (nil to use default) -- use_one (boolean): false: 2+1/2 → "two and a half"; true: "two and one-half" local function numeral_to_spanish(num, capitalize, ordinal, links, negative_word, round, zero) if not negative_word then negative_word = 'menos' end num = scientific_notation_to_decimal(num) if round and round ~= '' then if round ~= 'on' and round ~= 'up' and round ~= 'down' then error("Invalid rounding mode") end num = round_for_english(num, round) end -- Separar y detectar el signo negativo, num (dígitos antes del punto decimal), decimal_places (digitos después del decimal) -- Manejo de signos local MINUS = '−' -- Si utiliza el signo Unicode U+2212 MINUS SIGN reemplazarlo por el '-' if num:sub(1, #MINUS) == MINUS then num = '-' .. num:sub(#MINUS + 1) -- replace MINUS with '-' elseif num:sub(1, 1) == '+' then --Si encuentra un '+' ignorarlo num = num:sub(2) -- ignore any '+' end local negative = num:find("^%-") --Extraer el signo local decimal_places, subs = num:gsub("^%-?%d*%.(%d+)$", "%1") -- Extraer posiciones decimales if subs == 0 then decimal_places = nil end -- Si no hay marcarlo como nil num, subs = num:gsub("^%-?(%d*)%.?%d*$", "%1") -- Extraer el número if num == '' and decimal_places then num = '0' end -- Si está vacío y hay decimales -> marcar como 0 if subs == 0 or num == '' then error("Número decimal erróneo") end -- Si no se encuentra número se lanza un error -- For each group of 3 digits except the last one, print with appropriate group name (e.g. million) local s = '' -- Aquí se irá almacenando el número while #num > 6 do if s ~= '' then s = s .. ' ' end -- Si ya se han añadido cosas meter espacio local group_num = math.floor(#num/6) local group = groups[group_num] local group_digits_B = #num - group_num*6 local group_digits_A = group_digits_B-3 -- Los grupos son: XXXYYY000000 -- A B resto -- Manejando el grupo A if group_digits_A > 0 then if tonumber(num:sub(1, group_digits_A)) ~= 1 then local res = numeral_to_spanish_less_1000(num:sub(1, group_digits_A), false, zero, false):gsub('uno', 'un') s = s .. res .. ' ' end s = s .. 'mil ' else group_digits_A = 0 end -- Manejando el grupo B if tonumber(num:sub(group_digits_A+1, group_digits_B)) ~= 1 then if tonumber(num:sub(group_digits_A+1, group_digits_B)) ~= 0 then local res = numeral_to_spanish_less_1000(num:sub(group_digits_A+1, group_digits_B), false, zero, false):gsub('uno', 'un') s = s .. res .. ' ' end else s = s .. 'un ' end -- Añade la terminación al nombre de grupo en plural o singular if tonumber(num:sub(1, group_digits_B)) ~= 1 or group_digits_B == 6 then group = group .. 'llones' else group = group .. 'llón' end -- Cambia millon/millones por millonésimo en los ordinales que toque if ordinal and string.find(group, 'mill') and tonumber(num:sub(group_digits_B, group_digits_B+3)) == 0 then group = 'millonésimo' end -- Si el enlazado está activo coloca el enlace al grupo if links and (((links == 'on' and group_num >= 1) or links:find(group)) and group_num <= 13) then s = s .. '[[Orden_de_magnitud_(números)#10' .. group_num*6 .. '|' .. group .. ']]' else s = s .. group -- Si no lo deja tal cual end num = num:sub(1 + group_digits_B) num = num:gsub("^0*", "") -- Trim leading zeros end -- Handle final six digits of integer part if s ~= '' and num ~= '' then s = s .. ' ' end if s == '' or num ~= '' then local num_digits_A = 0 local isOnly = true if #num > 3 then local num_digits_A = #num - 3 if tonumber(num:sub(1, num_digits_A)) ~= 1 then if ordinal and tonumber(num:sub(1, num_digits_A)) < 100 then s = s .. numeral_to_spanish_less_1000(num:sub(1, num_digits_A), false, zero, false) else s = s .. numeral_to_spanish_less_1000(num:sub(1, num_digits_A), false, zero, false) .. ' ' end end if ordinal then s = s .. 'milésimo ' else s = s .. 'mil ' end num = num:sub(1 + num_digits_A) isOnly = false end s = s .. numeral_to_spanish_less_1000(num:sub(num_digits_A, #num), ordinal, zero, isOnly) end -- For decimal places (if any) output "point" followed by spelling out digit by digit if decimal_places then local name = '' local one = false if #decimal_places > 5 then local d = math.ceil((#decimal_places - 5)/6) name = groups[d] .. 'llonésima' else name = decimal_small_groups[#decimal_places] end if decimal_places ~= '1' then name = name .. 's' else one = true end s = s .. ' con' --for i = 1, #decimal_places do --s = s .. ' ' .. posicion_unidades[tonumber(decimal_places:sub(i,i))] --end while decimal_places:sub(1,1) == '0' do decimal_places = decimal_places:sub(2, #decimal_places) end s = s .. ' ' .. numeral_to_spanish(decimal_places, false, false, links, false, false, zero):gsub('uno', 'una'):gsub('cientos', 'cientas') s = s .. ' ' .. name end s = s:gsub("^%s*(.-)%s*$", "%1") -- Trim whitespace if negative and s ~= cero then s = negative_word .. ' ' .. s end s = s:gsub("menos cero", "cero") if capitalize then s = s:gsub("^%l", string.upper) end s = s:gsub("^%s*(.-)%s*$", "%1") -- Trim whitespace return s end ---- Función recursiva para convertir números decimales a hexadecimales local function decToHexDigit(dec) local dig = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"} local div = math.floor(dec/16) local mod = dec-(16*div) if div >= 1 then return decToHexDigit(div)..dig[mod+1] else return dig[mod+1] end end local p = { -- Functions that can be called from another module roman_to_numeral = roman_to_numeral, spell_number = numeral_to_spanish } function p._roman_to_numeral(frame) -- Callable via {{#invoke:ConvertNumeric|_roman_to_numeral|VI}} return roman_to_numeral(frame.args[1]) end function p.numeral_to_spanish(frame) local args = frame.args local num = args[1] num = num:gsub("^%s*(.-)%s*$", "%1") -- Quitar espacios en blanco num = num:gsub(",", "") -- Quitar comas num = num:gsub("^<span[^<>]*></span>", "") -- Generated by Template:age if num ~= '' then -- a fraction may have an empty whole number if not num:find("^%-?%d*%.?%d*%-?[Ee]?[+%-]?%d*$") then -- Input not in a valid format, try to pass it through #expr to see -- if that produces a number (e.g. "3 + 5" will become "8"). num = frame:preprocess('{{#expr: ' .. num .. '}}') end end -- Pass args from frame to helper function return numeral_to_spanish( num, args['case'] == 'U' or args['case'] == 'u', args['ord'] == 'on', args['lk'], args['negative'], args['round'], args['zero'] ) or '' end function p.decToHex(frame) local args=frame.args local parent=frame.getParent(frame) local pargs={} if parent then pargs=parent.args end local text=args[1] or pargs[1] or "" local minlength=args.minlength or pargs.minlength or 1 minlength=tonumber(minlength) local prowl=mw.ustring.gmatch(text,"(.-)(%d+)") local output="" repeat local chaff,dec=prowl() if not(dec) then break end local hex=decToHexDigit(dec) while (mw.ustring.len(hex)<minlength) do hex="0"..hex end output=output..chaff..hex until false local chaff=mw.ustring.match(text,"(%D+)$") or "" return output..chaff end return p