如何将整数转换为roman numerals

function romanNumeralGenerator (int) {

}

例如,请参见以下示例输入和输出:
1 = "I"
5 = "V"
10 = "X"
20 = "XX"
3999 = "MMMCMXCIX"

注意:仅支持1到3999之间的数字

最佳答案

我在使用Google找到的这个博客上有一个不错的网站:

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

关于javascript - 在JavaScript中将数字转换为罗马数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9083037/

10-12 07:37