showing results for - "how to convert numbers to roman numerals in javascript"
Lorena
19 Jul 2018
1function convertToRoman(num) {
2var numeral = "";
3 var arr = [
4 {number:1, roman: "I"},
5 {number:4, roman: "IV"},
6 {number:5, roman: "V"},
7 {number:9, roman: "IX"},
8 {number:10, roman: "X"},
9 {number:40, roman: "XL"},
10 {number:50, roman: "L"},
11 {number:90, roman: "XC"},
12 {number:100, roman: "C"},
13 {number:400, roman: "CD"},
14 {number:500, roman: "D"},
15 {number:900, roman: "CM"},
16 {number:1000, roman: "M"}
17 ]
18 while(num > 0){
19  var searching = arr.filter(function(myArr){
20    if(myArr.number <= num){
21      return myArr.number
22    }
23  })
24  var latest = searching.pop()
25  var full = Math.floor(num / latest.number)
26  for(let i = 0; i<full; i++){
27    numeral +=latest.roman
28  }
29  num = num % latest.number
30 }
31 return numeral 
32}
33
34console.log(convertToRoman(36));
Alma
28 Jun 2019
1function roman_to_Int(str1) {
2if(str1 == null) return -1;
3var num = char_to_int(str1.charAt(0));
4var pre, curr;
5
6for(var i = 1; i < str1.length; i++){
7curr = char_to_int(str1.charAt(i));
8pre = char_to_int(str1.charAt(i-1));
9if(curr <= pre){
10num += curr;
11} else {
12num = num - pre*2 + curr;
13}
14}
15
16return num;
17}
18
19function char_to_int(c){
20switch (c){
21case 'I': return 1;
22case 'V': return 5;
23case 'X': return 10;
24case 'L': return 50;
25case 'C': return 100;
26case 'D': return 500;
27case 'M': return 1000;
28default: return -1;
29}
30}
31console.log(roman_to_Int('XXVI'));
32console.log(roman_to_Int('CI'));
Filippo
07 Jan 2017
1class Solution {
2public:
3    int romanNumber(char n)
4    {
5        switch (n)
6        {
7        case 'I':
8                return (1);
9        case 'V':
10                return (5);
11        case 'X':
12                return (10);
13        case 'L':
14                return (50);
15        case 'C':
16                return (100);
17        case 'D':
18                return (500);
19        case 'M':
20                return (1000);
21        default:
22            return (0);
23            
24        }
25        return (0);
26    }
27    int romanToInt(string s) 
28    {
29        int len = s.size();
30        int ans = romanNumber(s[0]);
31        for(int i=1;i<len;i++)
32        {
33            int prev = romanNumber(s[i-1]);
34            int current = romanNumber(s[i]);
35            if(prev>=current)
36            {
37                ans += current;
38            }
39            else
40            {
41                ans -= prev;
42                current -= prev;
43                ans += current; 
44            }
45        }
46        return ans;
47    }
48};
queries leading to this page
how to write code in javascript roman numeral converternumbers to roman numerals javascriptinteger to roman javascriptconvert to roman numerals javascriptnumber to roman javascriptjavascript turn roman numerals into numbersjs int to roman to roman numbers jsnumber to roman numeral jsroman numerals to numbers in jsroman numeral converter en jsgiven a roman numeral n 2c convert it into integer in jsjavascript write a function to convert from normal numbers to roman numerals javascript roman numeral to integerconvert number to roman numbers jsconvert number to roman numeral jsjavascript translate roman numeralsconvert roman numeral to integer javascriptroman numbers convert javascriptjavascript convert number to roman numeralconvert integer to roman numeral javascriptjavascript number to roman numeralconvert number to roman numerals javascripthow to convert a number to a roman numeral typescriptroman numerals converter javascripttranslate roman numeral to number jsint to roman javascriptjs number to romanjavascript int to roman numeralconvert roman numerals to numbers jsroman numeral converter using javascripthow to change the roman number jsroman numeral converter jsroman numeral translator javascriptwrite a function to convert an integer to it e2 80 99s roman numeral equivalent javascripthow to convert numbers into roman numerals in javascriptdecima to roman jshow to convert to roman numerals javascripthow to get roman numerals to int in jsconvert the given number into a roman numeral jsconvert the given number into a roman numeral javascriptjs convert number to roman numeralroman numeral converter in javascriptconver numeric to roman jsjavascript convert roman numerals to numbersjavascript roman numeral converterinteger to roman numeral js roman numerals jshow to write a roman numeral converter in jsare there special functions in javascript for converting numbers to roman numeralnumber to roman in jsjs number to roman numeraljavascript replace roman numerals with numbersjavascript roman numeralsroman numerals to integers jsnumbers to roman javascriptconverting numbers to roman numerals jsconverting an integer into a roman numeral jshow to convert roman number to integer in javascriptconvert numbers to roman numerals javascriptroman numerals javascriptroman numerals converter jsconvert roman numerals to integers nodejsjavascript for roman numeral to intconvert decimal to roman js convert number to roman numeral javascriptjavascript numbers to roman numeralsroman numerical converter jsroman numeral javascriptroman numeral converter javascriptprolog convert numbers into roman numerals javascriptnumeral js from roman to numberroman to digit in javascriptconvert an integer to it e2 80 99s roman numeral equivalent javascriptjs function convert number to roman numeralconvert to roman numbers jsjavascript convert roman to numeralsjavascript int to romanint to roman jsnum to roman in javascripthow to convert roman numerals to numbers in javascriptare ther special functions in javascript for converting numbers to roman numeralgenerate roman numerals javascriptroman numbers to english numbers convert javascriptroman numeral encoder jsjs roman to numberroman numeral to integer javascriptconvert a number to roman numerals jshow to convert numbers to roman numerals in javascriptconvert roman numeral in jshow to change numbers to roman numeral is jsjavascript for roman numeral to number only using if elsenumber to roman numeral javascriptconvert roman to integer javascriptconvert number to roman numeral number in jsroman numerals to numbers javascriptjavascript convert numbers to roman numeralsconvert an integer to it e2 80 99s roman numeral equivalent javascript mediumhow to convert decimal number to roman number in javascroman numerals encoder javascriptconvert decimal to roman javascriptjavascript convert roman numberals to digitsroman numeral converter in jstranslate roman numerals into numbers jsjavascript convert number to roman numeralsconvert roman numerals to numbers javascriptroman numerals in javascriptintger to roman jsconvert a number to roman numerals in jsjs to roman numshow to convert decimal number to roman number in javascriptroman numeral jsnumeral js to roman to numberconvert roman numerals to numbers nodejsroman numeral to number javascriptint to roman en javascriptjs roman numeral converterjavascript integer to roman numeralsconvert integer to roman numerals nodejsroman to number javascriptjavascript roman numerals to integerconvert roman numeral to number javascriptroman numeral to decimal jsconvert roman numerals to numbers function javascripthow to convert number to roman numerals javascriptroman to num javascriptnumber to roman numeral converter jsnumbers to roman numbers jsroman numerals to number converter javascriptconvert numbers to roman numerals nodejshow to convert numbers to roman numerals in javascript