Cómo convertir de decimal a roman en javascript

Ejemplos de código

2
0

entero a la romana javascript

function convertToRoman(num) {
  var roman = {
    M: 1000,
    CM: 900,
    D: 500,
    CD: 400,
    C: 100,
    XC: 90,
    L: 50,
    XL: 40,
    X: 10,
    IX: 9,
    V: 5,
    IV: 4,
    I: 1
  };
  var str = '';

  for (var i of Object.keys(roman)) {
    var q = Math.floor(num / roman[i]);
    num -= q * roman[i];
    str += i.repeat(q);
  }

  return str;
}
0
0

cómo convertir de decimal a roman en javascript

  const decimalToRoman = () => {
    const intToRoman = (num) => {
      let result = "";
      while (num) {
        if (num >= 1000) {
          result += "M";
          num -= 1000;
        } else if (num >= 500) {
          if (num >= 900) {
            result += "CM";
            num -= 900;
          } else {
            result += "D";
            num -= 500;
          }
        } else if (num >= 100) {
          if (num >= 400) {
            result += "CD";
            num -= 400;
          } else {
            result += "C";
            num -= 100;
          }
        } else if (num >= 50) {
          if (num >= 90) {
            result += "XC";
            num -= 90;
          } else {
            result += "L";
            num -= 50;
          }
        } else if (num >= 10) {
          if (num >= 40) {
            result += "XL";
            num -= 40;
          } else {
            result += "X";
            num -= 10;
          }
        } else if (num >= 5) {
          if (num >= 9) {
            result += "IX";
            num -= 9;
          } else {
            result += "V";
            num -= 5;
          }
        } else {
          if (num >= 4) {
            result += "IV";
            num -= 4;
          } else {
            result += "I";
            num -= 1;
          }
        }
      }
      return result;
    };
    const newText = intToRoman(Math.abs(Number(string)));
    return newText;
  };

Páginas relacionadas

Páginas de ejemplo relacionadas

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................