Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
convert integer to roman numeral javascript
Object.defineProperty(Number.prototype,'int2roman',{
get: function(){
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,
};
let result = '';
let num=this.valueOf();
for (key in map) {
result += key.repeat(Math.floor(num / map[key]));
num %= map[key];
}
return result;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment