fbpx
Wikipedia

Módulo:ConvertNumeric/sandbox

Este módulo no tiene página de documentación[crear]
-- 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 

módulo, convertnumeric, sandbox, este, módulo, tiene, página, documentación, crear, módulo, convertnumeric, sandbox, código, discusión, tests, comprobar, tests, subpáginas, enlaces, módulo, para, convertir, entre, diferentes, representaciones, números, tests, . Este modulo no tiene pagina de documentacion crear Modulo ConvertNumeric sandbox codigo doc discusion tests comprobar tests subpaginas enlaces Modulo para convertir entre diferentes representaciones de numeros Los tests estan en Modulo ConvertNumeric tests usalos para verificar las ediciones Antes de editar el modulo prueba tus cambios en Modulo ConvertNumeric sandbox y verifica que funcionan en Modulo discusion 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 espanol una diferencia 1 primero 2 segundo 3 tercero 4 cuarto 5 quinto 6 sexto 7 septimo 8 octavo 9 noveno 10 decimo 11 decimo primero 12 duodecimo 13 decimo tercero 14 decimo cuarto 15 decimo quinto 16 decimo sexto 17 decimo septimo 18 decimo octavo 19 decimo 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 vigesimo 3 trigesimo 4 cuadragesimo 5 quincuagesimo 6 sexagesimo 7 septuagesimo 8 octogesimo 9 nonagesimo 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 centesimo 2 ducentesimo 3 tricentesimo 4 cuadringentesimo 5 quingentesimo 6 sexcentesimo 7 septingentesimo 8 octingentesimo 9 noningentesimo 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 decima 2 centesima 3 milesima 4 diezmilesima 5 cienmilesima 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 numero 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 numero romano contiene un caracter erroneo c end if next gt last then result result next raising true elseif next lt 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 numero entre 0 y 100 a texto en Espanol ejemplo 47 gt 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 lt 20 then Caso para numeros menores de 20 que son raros en general value unidades num elseif num 10 0 then Caso generico para veinte treinta cuarenta etc cuadragesimo 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 veintitres end value tmp else Caso generico 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 Espanol e g 475 gt 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 lt 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 numero expresado en notacion cientifica a la notacion decimal estandar ejemplo 1 23E5 gt 123000 1 23E 5 0000123 La conversion 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 gt 1 and mantissa sub 1 1 0 do mantissa mantissa sub 2 decimal pos decimal pos 1 end Shift decimal point right for exponent gt 0 while exponent gt 0 do decimal pos decimal pos 1 exponent exponent 1 if decimal pos gt mantissa 1 then mantissa mantissa 0 end Remove leading zeros unless decimal point is in first position while decimal pos gt 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 lt 0 while exponent lt 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 gt 5 then small int tostring tonumber small int 1 end return negative and or small int end When rounding up any number with gt 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 lt 20 and 1 rest 3 0 then In English numbers lt 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 gt 1 or round on and tonumber round digit gt 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 digitos antes del punto decimal decimal places digitos despues 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 numero if num and decimal places then num 0 end Si esta vacio y hay decimales gt marcar como 0 if subs 0 or num then error Numero decimal erroneo end Si no se encuentra numero se lanza un error For each group of 3 digits except the last one print with appropriate group name e g million local s Aqui se ira almacenando el numero while num gt 6 do if s then s s end Si ya se han anadido 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 gt 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 Anade la terminacion 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 llon end Cambia millon millones por millonesimo 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 millonesimo end Si el enlazado esta activo coloca el enlace al grupo if links and links on and group num gt 1 or links find group and group num lt 13 then s s Orden de magnitud numeros 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 gt 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 lt 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 milesimo 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 gt 5 then local d math ceil decimal places 5 6 name groups d llonesima 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 Funcion recursiva para convertir numeros 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 gt 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 lt span lt gt gt lt span gt 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 lt 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 pObtenido de https es wikipedia org w index php title Modulo ConvertNumeric sandbox amp oldid 129362057, wikipedia, wiki, leyendo, leer, libro, biblioteca,

español

, española, descargar, gratis, descargar gratis, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, imagen, música, canción, película, libro, juego, juegos