Created
July 14, 2015 15:14
-
-
Save renesansz/f0ec47e03f9bc794ae37 to your computer and use it in GitHub Desktop.
Decimal to Roman Numeral Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function DecimalToRoman(num) { | |
if (num <= 0 || num >= 4000) return num; | |
var roman = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]; | |
var decimal = [1000,900,500,400,100,90,50,40,10,9,5,4,1]; | |
var romanStr = ''; | |
for (var i = 0, limit = roman.length; i < limit; ++i) { | |
while (num >= decimal[i]) { | |
num -= decimal[i]; | |
romanStr += roman[i]; | |
} | |
} | |
return romanStr; | |
} | |
console.log(convert(36)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment