Skip to content

Instantly share code, notes, and snippets.

@CyberGen49
Last active May 9, 2022 09:31
Show Gist options
  • Save CyberGen49/ba9c4ac27aadebe2275799170e434f84 to your computer and use it in GitHub Desktop.
Save CyberGen49/ba9c4ac27aadebe2275799170e434f84 to your computer and use it in GitHub Desktop.
Integer to Roman Numerals
function numeral(num) {
const ones = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];
const tens = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'];
const huns = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'];
return [
'X̅'.repeat(Math.floor(num/10000)),
'M'.repeat(Math.floor((num%10000)/1000)),
huns[Math.floor((num%1000)/100)],
tens[Math.floor(((num%1000)%100)/10)],
ones[Math.floor(((num%1000)%100)%10)]
].join('');
};
console.log(numeral(12345)); // X̅MMCCCXLV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment