How to convert a decimal number to roman using JavaScript?


Roman numerals are a type of number system used to represent a fixed decimal number. Several letters from the Latin alphabet are used for the representation of roman numerals. In this tutorial, the user will learn how to convert a decimal number to roman using JavaScript. Seven symbols represent Roman numerals: I, V, X, L, C, D, and M. The values for symbols are given below.

Syntax

I is 1
V is 5
X is 10
L is 50
C is 100
D is 500
M is 1000

Using these seven symbols user can convert decimal numbers to roman.

Users can follow the below syntax to convert a decimal number to a roman numeral.

Syntax

function getRoman(num) {
   //traverse in roman_numerals array using for loop
   if (num == 0)
      break;
   while (num >= decimal_number[i]) {
      num -= decimal_number[i];
      romanNumeral += roman_numeral[i];
      if (roman_numeral[i] == 'M')
      number_thousands++;
   }
}

function decimal_to_roman(num) {
// check number is between 1 to 3888888
   if (num <= 0 || num >= 3888888)
   {
      // can not convert to roman number
   }
   // it will call getRoman() function
   if (roman1.number_thousands > 4) {
      var temp=roman1.number_thousands;
      while(temp>0){
         thous_string += "M";
         temp--;
      }
      var thousands1 = getRoman(roman1.number_thousands);
      var thousandBase = "<span style='border-top:1px solid #000'>" +
      thousands1.romanNumeral + "</span>";
      romanNumeral = roman1.romanNumeral.replace(thous_string, thousandBase);
   }
   else romanNumeral = roman1.romanNumeral;
}

Users can follow the below algorithm to get a roman numeral.

Algorithm

STEP 1 − Initialize an array roman_numeral and it contains elements as roman symbols.

STEP 2 − Call decimal_to_roman() function with parameter num.

STEP 3 − Check whether the num is within the range or not.

STEP 4 − If the num is in the range call getRoman(). In this function traverse using for loop in roman_numeral.

STEP 5 − If num=0 break, else while num is greater than or equal to current roman value, add corresponding roman symbol to romanNumeral string. If current roman symbol is ‘M’, increase count of number_thousands by 1.

STEP 6getRoman() returns value to decimal_to_roman().

STEP 7 − If number_thousands <= 4, print romanNumeral.

STEP 8 − If number_thousands > 4, make stringthous_string of length equal to number_thousands containing symbol M. Find roman numeral for number_thousands using getRoman( ) then add bar over it and store in thousandBase.

STEP 9 − In romanNumeral replace thous_string to thousandBase string.

STEP 10 − Print romanNumeral.

User can follow below example for better understanding.

Example 1

A number in Roman Numerals is a string of seven symbols. In this example, the user will learn to convert a decimal number to a roman using two arrays: roman_numeral[ ] and decimal_number[ ].

The value for the roman numeral,

  • CM is 900
  • CD is 400
  • XC is 90
  • XL is 40
  • IX is 9
  • IV is 4.
<html> <head> </head> <body> <h2> Convert a decimal number to roman using JavaScript. </h2> <h4> After converting <i> decimal number to roman </i>. </h4> <p id = "div1"> </p> <p id = "div2"> </p> <script> let Div1 = document.getElementById("div1"); let Div2 = document.getElementById("div2"); //declaring roman symbols array var roman_numeral = new Array(); roman_numeral = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]; //declaring array of values for roman symbols var decimal_number = new Array(); decimal_number = [1000,900,500,400,100,90,50,40,10,9,5,4,1]; function getRoman(num) { var romanNumeral = ""; var number_thousands = 0; for (var i=0; i< roman_numeral.length; i++) { if (num == 0) break; while (num >= decimal_number[i]) { num -= decimal_number[i]; romanNumeral += roman_numeral[i]; if (roman_numeral[i] == 'M') number_thousands++; } } return { number_thousands:number_thousands, romanNumeral:romanNumeral }; } function decimal_to_roman(num) { // 3,888,888 is the longest number represented by Roman numerals if (num <= 0 || num > 3888888) return num; var romanNumeral = ""; var roman1 = getRoman(num); // If the number is 4000 or greater if (roman1.number_thousands > 4) { var thous_string = ""; var temp=roman1.number_thousands; while(temp>0){ thous_string += "M"; temp--; } var thousands1 = getRoman(roman1.number_thousands); var thousandBase = "<span style='border-top:1px solid #000'>" + thousands1.romanNumeral + "</span>"; romanNumeral = roman1.romanNumeral.replace(thous_string, thousandBase); } else romanNumeral = roman1.romanNumeral; return romanNumeral; } Div1.innerHTML="Roman conversion of number 577 is " + decimal_to_roman(577); Div2.innerHTML="Roman conversion of number 3542 is " + decimal_to_roman(3542); </script> </body> </html>

In above example, user is getting roman numerals of numbers 577 and 3542 as DLXXVII and MMMDXLII respectively.

Example 2

In the below example, we take the input from the user, and on clicking the button, the decimal number is converted to Roman Numeral.

<html> <body> <h2> Convert a decimal number to roman using JavaScript. </h2> <p> Click the "Convert Decimal to Roman" button after entering the number to convert into Roman Numeral </p> <p> Enter Decimal Number: <input type="number" , value='50' , id="decimal"></p> <button onclick="decimal_to_roman()">Convert Decimal to Roman</button> <p id="div1"> </p> <script> let Div1 = document.getElementById("div1"); //declaring roman symbols array var roman_numeral = new Array(); roman_numeral = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]; //declaring array of values for roman symbols var decimal_number = new Array(); decimal_number = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; function getRoman(num) { var romanNumeral = ""; var number_thousands = 0; for (var i = 0; i < roman_numeral.length; i++) { if (num == 0) break; while (num >= decimal_number[i]) { num -= decimal_number[i]; romanNumeral += roman_numeral[i]; if (roman_numeral[i] == 'M') number_thousands++; } } return { number_thousands: number_thousands, romanNumeral: romanNumeral }; } function decimal_to_roman() { const num = document.getElementById("decimal").value; // 3,888,888 is the longest number represented by Roman numerals if (num <= 0 || num > 3888888) return num; var romanNumeral = ""; var roman1 = getRoman(num); // If the number is 4000 or greater if (roman1.number_thousands > 4) { var thous_string = ""; var temp = roman1.number_thousands; while (temp > 0) { thous_string += "M"; temp--; } var thousands1 = getRoman(roman1.number_thousands); var thousandBase = "<span style='border-top:1px solid #000'>" + thousands1.romanNumeral + "</span>"; romanNumeral = roman1.romanNumeral.replace(thous_string, thousandBase); } else romanNumeral = roman1.romanNumeral; // return romanNumeral; Div1.innerHTML = romanNumeral; } </script> </body> </html>

Using JavaScript, the user learned how to convert any decimal number into roman numerals. The user learned that when the number is above a specific range, the representation of decimal numbers in roman is quite different. Users have to place a bar over the base numeral in this situation.

Updated on: 22-Aug-2022

566 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements