convert to roman numerals javascript code example
Example 1: convert into roman js
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));
Example 2: convert roman numeral to number javascript
class Solution {
public:
int romanNumber(char n)
{
switch (n)
{
case 'I':
return (1);
case 'V':
return (5);
case 'X':
return (10);
case 'L':
return (50);
case 'C':
return (100);
case 'D':
return (500);
case 'M':
return (1000);
default:
return (0);
}
return (0);
}
int romanToInt(string s)
{
int len = s.size();
int ans = romanNumber(s[0]);
for(int i=1;i<len;i++)
{
int prev = romanNumber(s[i-1]);
int current = romanNumber(s[i]);
if(prev>=current)
{
ans += current;
}
else
{
ans -= prev;
current -= prev;
ans += current;
}
}
return ans;
}
};
Example 3: convert roman numeral to number javascript
function roman_to_Int(str1) {
if(str1 == null) return -1;
var num = char_to_int(str1.charAt(0));
var pre, curr;
for(var i = 1; i < str1.length; i++){
curr = char_to_int(str1.charAt(i));
pre = char_to_int(str1.charAt(i-1));
if(curr <= pre){
num += curr;
} else {
num = num - pre*2 + curr;
}
}
return num;
}
function char_to_int(c){
switch (c){
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return -1;
}
}
console.log(roman_to_Int('XXVI'));
console.log(roman_to_Int('CI'));