programming-examples/js/Math/Convert an integer into a Roman Numeral.js
2019-11-15 12:59:38 +01:00

16 lines
581 B
JavaScript

function integer_to_roman(num) {
if (typeof num !== 'number')
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_num = "",
i = 3;
while (i--)
roman_num = (key[+digits.pop() + (i * 10)] || "") + roman_num;
return Array(+digits.join("") + 1).join("M") + roman_num;
}
console.log(integer_to_roman(27));