Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var num = @Number
- const NUM_NAMES = ["cero", "uno", "dos", "tres", "cuatro", "cinco",
- "seis", "siete", "ocho", "nueve",
- "diez", "once", "doce", "trece", "catorce",
- "quince", "dieciséis", "diecisiete", "dieciocho", "diecinueve", "veinte",
- "veintiuno", "veintidós", "veintitrés", "veinticuatro", "veinticinco",
- "veintiséis", "veintisiete", "veintiocho", "veintinueve"
- ];
- const TENS_NAMES = ["", "diez", "veinte", "treinta", "cuarenta", "cincuenta",
- "sesenta", "setenta", "ochenta", "noventa", "ciento"
- ];
- const HUNDREDS_NAMES = ["", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos",
- "seiscientos", "setecientos", "ochocientos", "novecientos"
- ];
- const POWER_NAMES = ["mil", "millónes", "mil millónes", "billónes", "mil billónes", "trillónes"];
- const SINGLE_POWER_NAMES = ["un mil", "un millón", "mil millónes", "un billón", "mil billónes", "un trillón"];
- function convertLessThanOneHundred(num) {
- if (num < 30) {
- return " " + NUM_NAMES[num];
- } else {
- var tens = " " + TENS_NAMES[Math.floor(num / 10)];
- num %= 10;
- if (num != 0) {
- var ones = " y ";
- ones = ones + NUM_NAMES[num];
- return tens + ones;
- }
- return tens;
- }
- }
- function convertLessThanOneThousand(num) {
- if (num == 100) {
- return " cien";
- } else {
- var hun = " " + HUNDREDS_NAMES[Math.floor(num / 100)];
- var rem = convertLessThanOneHundred(num % 100);
- return hun + rem;
- }
- }
- function convertPower(num, power) {
- if (num != 0) {
- var powPart = "";
- if (num == 1) {
- powPart = SINGLE_POWER_NAMES[power];
- } else {
- powPart = convertLessThanOneThousand(num) + " " +
- POWER_NAMES[power];
- }
- return powPart;
- }
- return "";
- }
- function convert(num) {
- var ans = "";
- if (num < 0) {
- num = -num;
- ans += " menos";
- }
- var textNum = num.toString();
- var numberTooLarge = Number(textNum.slice(-22, -21));
- var quintillions = Number(textNum.slice(-21, -18));
- var quadrillions = Number(textNum.slice(-18, -15));
- var trillions = Number(textNum.slice(-15, -12));
- var billions = Number(textNum.slice(-12, -9));
- var millions = Number(textNum.slice(-9, -6));
- var thousands = Number(textNum.slice(-6, -3));
- var hundreds = Number(textNum.slice(-3));
- if (numberTooLarge != 0) {
- ans = "Number too large!";
- return ans;
- }
- if (quintillions != 0) {
- ans += convertPower(quintillions, 5);
- }
- if (quadrillions != 0) {
- ans += convertPower(quadrillions, 4);
- }
- if (trillions != 0) {
- ans += convertPower(trillions, 3);
- }
- if (billions != 0) {
- ans += convertPower(billions, 2);
- }
- if (millions != 0) {
- ans += convertPower(millions, 1);
- }
- if (thousands != 0) {
- ans += convertPower(thousands, 0);
- }
- ans += convertLessThanOneThousand(hundreds);
- return ans;
- }
- convert(num);
Advertisement
Add Comment
Please, Sign In to add comment