iizy

numberToSpanishPodio

Sep 4th, 2024 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var num = @Number
  2.  
  3.  
  4. const NUM_NAMES = ["cero", "uno", "dos", "tres", "cuatro", "cinco",
  5.   "seis", "siete", "ocho", "nueve",
  6.   "diez", "once", "doce", "trece", "catorce",
  7.   "quince", "dieciséis", "diecisiete", "dieciocho", "diecinueve", "veinte",
  8.   "veintiuno", "veintidós", "veintitrés", "veinticuatro", "veinticinco",
  9.   "veintiséis", "veintisiete", "veintiocho", "veintinueve"
  10. ];
  11.  
  12. const TENS_NAMES = ["", "diez", "veinte", "treinta", "cuarenta", "cincuenta",
  13.   "sesenta", "setenta", "ochenta", "noventa", "ciento"
  14. ];
  15.  
  16. const HUNDREDS_NAMES = ["", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos",
  17.   "seiscientos", "setecientos", "ochocientos", "novecientos"
  18. ];
  19.  
  20. const POWER_NAMES = ["mil", "millónes", "mil millónes", "billónes", "mil billónes", "trillónes"];
  21.  
  22. const SINGLE_POWER_NAMES = ["un mil", "un millón", "mil millónes", "un billón", "mil billónes", "un trillón"];
  23.  
  24. function convertLessThanOneHundred(num) {
  25.   if (num < 30) {
  26.     return " " + NUM_NAMES[num];
  27.   } else {
  28.     var tens = " " + TENS_NAMES[Math.floor(num / 10)];
  29.     num %= 10;
  30.     if (num != 0) {
  31.       var ones = " y ";
  32.       ones = ones + NUM_NAMES[num];
  33.       return tens + ones;
  34.     }
  35.     return tens;
  36.   }
  37. }
  38.  
  39. function convertLessThanOneThousand(num) {
  40.   if (num == 100) {
  41.     return " cien";
  42.   } else {
  43.     var hun = " " + HUNDREDS_NAMES[Math.floor(num / 100)];
  44.     var rem = convertLessThanOneHundred(num % 100);
  45.     return hun + rem;
  46.   }
  47. }
  48.  
  49. function convertPower(num, power) {
  50.   if (num != 0) {
  51.     var powPart = "";
  52.     if (num == 1) {
  53.       powPart = SINGLE_POWER_NAMES[power];
  54.     } else {
  55.       powPart = convertLessThanOneThousand(num) + " " +
  56.         POWER_NAMES[power];
  57.     }
  58.     return powPart;
  59.   }
  60.   return "";
  61. }
  62.  
  63. function convert(num) {
  64.   var ans = "";
  65.  
  66.   if (num < 0) {
  67.     num = -num;
  68.     ans += " menos";
  69.   }
  70.   var textNum = num.toString();
  71.  
  72.   var numberTooLarge = Number(textNum.slice(-22, -21));
  73.   var quintillions = Number(textNum.slice(-21, -18));
  74.   var quadrillions = Number(textNum.slice(-18, -15));
  75.   var trillions = Number(textNum.slice(-15, -12));
  76.   var billions = Number(textNum.slice(-12, -9));
  77.   var millions = Number(textNum.slice(-9, -6));
  78.   var thousands = Number(textNum.slice(-6, -3));
  79.   var hundreds = Number(textNum.slice(-3));
  80.  
  81.   if (numberTooLarge != 0) {
  82.     ans = "Number too large!";
  83.     return ans;
  84.   }
  85.   if (quintillions != 0) {
  86.     ans += convertPower(quintillions, 5);
  87.   }
  88.   if (quadrillions != 0) {
  89.     ans += convertPower(quadrillions, 4);
  90.   }
  91.  
  92.   if (trillions != 0) {
  93.     ans += convertPower(trillions, 3);
  94.   }
  95.  
  96.   if (billions != 0) {
  97.     ans += convertPower(billions, 2);
  98.   }
  99.  
  100.   if (millions != 0) {
  101.     ans += convertPower(millions, 1);
  102.   }
  103.  
  104.   if (thousands != 0) {
  105.     ans += convertPower(thousands, 0);
  106.   }
  107.  
  108.   ans += convertLessThanOneThousand(hundreds);
  109.  
  110.   return ans;
  111. }
  112.  
  113. convert(num);
  114.  
Advertisement
Add Comment
Please, Sign In to add comment