在javaScript中将数字转换为罗马数字

DD7*_*D77 50 javascript roman-numerals

如何将整数转换为罗马数字

function romanNumeralGenerator (int) {

}
Run Code Online (Sandbox Code Playgroud)

例如,请参阅以下示例输入和输出:

1 = "I"
5 = "V"
10 = "X"
20 = "XX"
3999 = "MMMCMXCIX"
Run Code Online (Sandbox Code Playgroud)

警告:仅支持1到3999之间的数字

Ren*_*Pot 81

我发现使用谷歌在这个博客上有一个很好的:

http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter

function romanize (num) {
    if (isNaN(num))
        return NaN;
    var digits = String(+num).split(""),
        key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
               "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
               "","I","II","III","IV","V","VI","VII","VIII","IX"],
        roman = "",
        i = 3;
    while (i--)
        roman = (key[+digits.pop() + (i * 10)] || "") + roman;
    return Array(+digits.join("") + 1).join("M") + roman;
}
Run Code Online (Sandbox Code Playgroud)

  • @tukusejssirs 幸运的是,罗马数字是用于年份的,否则会有大于 1000 的数字。但很高兴知道! (3认同)
  • 提醒:这应该更好地返回“NaN”或抛出而不是返回“false”,如该文章中讨论的那样。 (2认同)

jag*_*oft 63

function romanize(num) {
  var lookup = {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},roman = '',i;
  for ( i in lookup ) {
    while ( num >= lookup[i] ) {
      roman += i;
      num -= lookup[i];
    }
  }
  return roman;
}
Run Code Online (Sandbox Code Playgroud)

转自2008年的评论:http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter

查看演示

  • 物品没有订单!你应该使用一个数组并避免`for ... in`. (10认同)
  • 我不能给出一个非工作的例子,因为订单是依赖于实现的.相反,请链接我的规范的哪一部分确保它将以所需的顺序迭代.[哦,你不能](http://stackoverflow.com/q/30076219/1529630). (7认同)
  • 这很好,在解析这里发生的事情似乎在心理上更容易.从最大数字开始,继续从查找表中减去并追加,只要余数大于查找值. (3认同)
  • 我不是在谈论性能。我是说迭代顺序没有保证,所以结果可能完全错误。 (3认同)
  • @jaggedsoft 这是一个在某些浏览器中更改顺序的示例:`for (let i in { '2': 2, '1': 1 }) console.log(i);`。此外,即使您对其进行了测试,也不意味着它始终有效。可能存在浏览器出于性能原因或任何原因更改顺序的情况。您只能确定,如果您知道您支持并希望将来支持的每个浏览器版本的源代码。 (3认同)
  • [更新,2021 年 6 月](/sf/answers/2164332761/):从 ES2020 开始,即使在 `for...in` 循环中,对象迭代顺序也保证与对象的属性顺序相同。为了回应@Oriol,[对象确实有“顺序”](https://stackoverflow.com/revisions/30919039/5),从 ES2015 开始,有点迂腐,但它们的_迭代顺序_不能保证与直到最近,使用“for...in”时的属性顺序。 (3认同)
  • 不错,花了我一秒时间才明白,复杂度如何 (2认同)
  • 对象没有顺序,所以我将它实现为对列表`[{M:1000}, ...]` (2认同)

Aug*_*ust 39

我不明白为什么每个人的解决方案都这么长,并且使用多个for循环.

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;
}
Run Code Online (Sandbox Code Playgroud)


ken*_*bec 14

这些函数将任何正整数转换为其等效的罗马数字字符串; 和任何罗马数字的数字.

罗马数字的数字:

Number.prototype.toRoman= function () {
    var num = Math.floor(this), 
        val, s= '', i= 0, 
        v = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1], 
        r = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']; 

    function toBigRoman(n) {
        var ret = '', n1 = '', rem = n;
        while (rem > 1000) {
            var prefix = '', suffix = '', n = rem, s = '' + rem, magnitude = 1;
            while (n > 1000) {
                n /= 1000;
                magnitude *= 1000;
                prefix += '(';
                suffix += ')';
            }
            n1 = Math.floor(n);
            rem = s - (n1 * magnitude);
            ret += prefix + n1.toRoman() + suffix;
        }
        return ret + rem.toRoman();
    }

    if (this - num || num < 1) num = 0;
    if (num > 3999) return toBigRoman(num);

    while (num) {
        val = v[i];
        while (num >= val) {
            num -= val;
            s += r[i];
        }
        ++i;
    }
    return s;
};
Run Code Online (Sandbox Code Playgroud)

罗马数字字符串到数字:

Number.fromRoman = function (roman, accept) {
    var s = roman.toUpperCase().replace(/ +/g, ''), 
        L = s.length, sum = 0, i = 0, next, val, 
        R = { M: 1000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 };

    function fromBigRoman(rn) {
        var n = 0, x, n1, S, rx =/(\(*)([MDCLXVI]+)/g;

        while ((S = rx.exec(rn)) != null) {
            x = S[1].length;
            n1 = Number.fromRoman(S[2])
            if (isNaN(n1)) return NaN;
            if (x) n1 *= Math.pow(1000, x);
            n += n1;
        }
        return n;
    }

    if (/^[MDCLXVI)(]+$/.test(s)) {
        if (s.indexOf('(') == 0) return fromBigRoman(s);

        while (i < L) {
            val = R[s.charAt(i++)];
            next = R[s.charAt(i)] || 0;
            if (next - val > 0) val *= -1;
            sum += val;
        }
        if (accept || sum.toRoman() === s) return sum;
    }
    return NaN;
};
Run Code Online (Sandbox Code Playgroud)


Pio*_*cki 13

我已经开发了下面的递归解决方案.该函数返回一个字母,然后调用自身返回下一个字母.它会一直传递给函数,0这意味着所有字母都已找到,我们可以退出递归.

var romanMatrix = [
  [1000, 'M'],
  [900, 'CM'],
  [500, 'D'],
  [400, 'CD'],
  [100, 'C'],
  [90, 'XC'],
  [50, 'L'],
  [40, 'XL'],
  [10, 'X'],
  [9, 'IX'],
  [5, 'V'],
  [4, 'IV'],
  [1, 'I']
];

function convertToRoman(num) {
  if (num === 0) {
    return '';
  }
  for (var i = 0; i < romanMatrix.length; i++) {
    if (num >= romanMatrix[i][0]) {
      return romanMatrix[i][1] + convertToRoman(num - romanMatrix[i][0]);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我非常喜欢你的解决方案.易于阅读,理解和非常简单.太好了! (3认同)

Def*_*wun 8

我知道这是一个老问题,但我为这个解决方案感到非常自豪:)它只处理小于1000的数字,但可以通过添加到'numeralCodes'的2D数组轻松扩展到包括你需要的大数.

var numeralCodes = [["","I","II","III","IV","V","VI","VII","VIII","IX"],         // Ones
                    ["","X","XX","XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"],   // Tens
                    ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"]];        // Hundreds

function convert(num) {
  var numeral = "";
  var digits = num.toString().split('').reverse();
  for (var i=0; i < digits.length; i++){
    numeral = numeralCodes[i][parseInt(digits[i])] + numeral;
  }
  return numeral;  
}
Run Code Online (Sandbox Code Playgroud)
<input id="text-input" type="text">
<button id="convert-button" onClick="var n = parseInt(document.getElementById('text-input').value);document.getElementById('text-output').value = convert(n);">Convert!</button>
<input id="text-output" style="display:block" type="text">
Run Code Online (Sandbox Code Playgroud)


Sha*_*awn 7

我知道这是一个过时的问题,但我有此处列出的最短解决方案,我想我会分享,因为我认为它更容易理解。

此版本不需要任何硬编码逻辑来处理边缘情况,例如 4(IV)、9(IX)、40(XL)、900(CM) 等,就像其他版本一样。这也意味着它可以处理比最大罗马刻度(3999)更大的数字。例如,如果“T”成为一个新符号,您可以将它添加到 romanLookup 对象的开头,它会保持相同的算法效果;或者当然假设“一行中不超过 3 个相同的符号”规则适用。

我已经针对 1-3999 的数据集对此进行了测试,并且可以完美运行。

function convertToRoman(num) {
  //create key:value pairs
  var romanLookup = {M:1000, D:500, C:100, L:50, X:10, V:5, I:1};
  var roman = [];
  var romanKeys = Object.keys(romanLookup);
  var curValue;
  var index;
  var count = 1;

  for(var numeral in romanLookup){
    curValue = romanLookup[numeral];
    index = romanKeys.indexOf(numeral);

    while(num >= curValue){

      if(count < 4){
        //push up to 3 of the same numeral
        roman.push(numeral);
      } else {
        //else we had to push four, so we need to convert the numerals 
        //to the next highest denomination "coloring-up in poker speak"

        //Note: We need to check previous index because it might be part of the current number.
        //Example:(9) would attempt (VIIII) so we would need to remove the V as well as the I's
        //otherwise removing just the last three III would be incorrect, because the swap 
        //would give us (VIX) instead of the correct answer (IX)
        if(roman.indexOf(romanKeys[index - 1]) > -1){
          //remove the previous numeral we worked with 
          //and everything after it since we will replace them
          roman.splice(roman.indexOf(romanKeys[index - 1]));
          //push the current numeral and the one that appeared two iterations ago; 
          //think (IX) where we skip (V)
          roman.push(romanKeys[index], romanKeys[index - 2]);
        } else {
          //else Example:(4) would attemt (IIII) so remove three I's and replace with a V 
          //to get the correct answer of (IV)

          //remove the last 3 numerals which are all the same
          roman.splice(-3);
          //push the current numeral and the one that appeared right before it; think (IV)
          roman.push(romanKeys[index], romanKeys[index - 1]);
        }
      }
      //reduce our number by the value we already converted to a numeral
      num -= curValue;
      count++;
    }
    count = 1;
  }
  return roman.join("");
}

convertToRoman(36);
Run Code Online (Sandbox Code Playgroud)

  • 为什么这会被否决?这是一个按原样工作的示例,可以回答问题并且有很好的文档记录? (2认同)

Kor*_*rad 7

我创建了两个转换函数。

第一个函数可以使用reduce 将数字转换为罗马数字。第二个函数与第一个函数非常相似,函数使用相同的方式来转换值。

您需要更改的一切都是_roman属性。因为你必须按照你想要的规模扩展这个常量,所以我在那里放置了最大数量1000,但你可以放更多。

您可以在这里找到带有罗马数字的更大比例https://www.tuomas.salste.net/doc/roman/numeri-romani.html

const _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 };

// 1903 => MCMIII
function toRoman(number = 0) {
    return Object.keys(_roman).reduce((acc, key) => {
        while (number >= _roman[key]) {
            acc += key;
            number -= _roman[key];
        }
        return acc;
    }, '');
}


// MCMIII => 1903
function fromRoman(roman = '') {
    return Object.keys(_roman).reduce((acc, key) => {
        while (roman.indexOf(key) === 0) {
            acc += _roman[key];
            roman = roman.substr(key.length);
        }
        return acc;
    }, 0);
}

console.log(toRoman(1903));  // should return 'MCMIII
console.log(fromRoman('MCMIII')); // should return 1903
Run Code Online (Sandbox Code Playgroud)


小智 6

这是递归的解决方案,看起来很简单:

const toRoman = (num, result = '') => {
    const map = {
        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,
      };
      for (const key in map) {
        if (num >= map[key]) {
          if (num !== 0) {
            return toRoman(num - map[key], result + key);
          }
        }
      }
      return result;
    };
console.log(toRoman(402)); // CDII
console.log(toRoman(3000)); // MMM
console.log(toRoman(93)); // XCIII
console.log(toRoman(4)); // IV
Run Code Online (Sandbox Code Playgroud)


Sen*_*kin 5

JavaScript的

function romanize (num) {
    if (!+num)
        return false;
    var digits = String(+num).split(""),
        key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
               "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
               "","I","II","III","IV","V","VI","VII","VIII","IX"],
        roman = "",
        i = 3;
    while (i--)
        roman = (key[+digits.pop() + (i * 10)] || "") + roman;
    return Array(+digits.join("") + 1).join("M") + roman;
}
Run Code Online (Sandbox Code Playgroud)

许多其他建议可以在http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter找到


ben*_*bia 5

我个人认为,最简洁的方法(绝不是最快的方法)是递归。

function convert(num) { 
  if(num < 1){ return "";}
  if(num >= 40){ return "XL" + convert(num - 40);}
  if(num >= 10){ return "X" + convert(num - 10);}
  if(num >= 9){ return "IX" + convert(num - 9);}
  if(num >= 5){ return "V" + convert(num - 5);}
  if(num >= 4){ return "IV" + convert(num - 4);}
  if(num >= 1){ return "I" + convert(num - 1);}  
}
console.log(convert(39)); 
//Output: XXXIX
Run Code Online (Sandbox Code Playgroud)

这仅支持数字1-40,但是可以通过遵循模式轻松扩展。


Lou*_*Bao 5

循环可能更优雅,但我发现它们很难阅读。想出了一个或多或少的硬编码版本,这对眼睛来说很容易。只要你理解第一行,剩下的就很简单了。

function romanNumeralGenerator (int) {
  let roman = '';

  roman +=  'M'.repeat(int / 1000);  int %= 1000; 
  roman += 'CM'.repeat(int / 900);   int %= 900; 
  roman +=  'D'.repeat(int / 500);   int %= 500;  
  roman += 'CD'.repeat(int / 400);   int %= 400;
  roman +=  'C'.repeat(int / 100);   int %= 100;
  roman += 'XC'.repeat(int / 90);    int %= 90;
  roman +=  'L'.repeat(int / 50);    int %= 50;
  roman += 'XL'.repeat(int / 40);    int %= 40;
  roman +=  'X'.repeat(int / 10);    int %= 10;
  roman += 'IX'.repeat(int / 9);     int %= 9;
  roman +=  'V'.repeat(int / 5);     int %= 5;
  roman += 'IV'.repeat(int / 4);     int %= 4;
  roman +=  'I'.repeat(int);

  return roman;
}
Run Code Online (Sandbox Code Playgroud)