如何使用 JavaScript 將十進位制數字轉換為羅馬數字?


羅馬數字是一種用於表示固定十進位制數字的數字系統。拉丁字母中的幾個字母用於表示羅馬數字。在本教程中,使用者將學習如何使用 JavaScript 將十進位制數字轉換為羅馬數字。七個符號表示羅馬數字:I、V、X、L、C、D 和 M。符號的值如下所示。

語法

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

使用這七個符號,使用者可以將十進位制數字轉換為羅馬數字。

使用者可以按照以下語法將十進位制數字轉換為羅馬數字。

語法

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;
}

使用者可以按照以下演算法獲取羅馬數字。

演算法

步驟 1 - 初始化一個數組 roman_numeral,其中包含羅馬符號作為元素。

步驟 2 - 使用引數 num 呼叫 decimal_to_roman() 函式。

步驟 3 - 檢查 num 是否在範圍內。

步驟 4 - 如果 num 在範圍內,則呼叫 getRoman()。在此函式中,使用 for 迴圈遍歷 roman_numeral

步驟 5 - 如果 num=0 則中斷,否則當 num 大於或等於當前羅馬值時,將相應的羅馬符號新增到 romanNumeral 字串中。如果當前羅馬符號為“M”,則將 number_thousands 的計數增加 1。

步驟 6 - getRoman() 將值返回給 decimal_to_roman()。

步驟 7 - 如果 number_thousands <= 4,則列印 romanNumeral

步驟 8 - 如果 number_thousands > 4,則建立一個長度等於 number_thousands 的字串thous_string,其中包含符號 M。使用 getRoman( ) 查詢 number_thousands 的羅馬數字,然後在其上新增橫線並存儲在 thousandBase 中。

步驟 9 - 在 romanNumeral 中將 thous_string 替換為 thousandBase 字串。

步驟 10 - 列印 romanNumeral

使用者可以參考以下示例以更好地理解。

示例 1

羅馬數字中的數字是一個由七個符號組成的字串。在本例中,使用者將學習使用兩個陣列:roman_numeral[ ] 和 decimal_number[ ] 將十進位制數字轉換為羅馬數字。

羅馬數字的值為:

  • CM 為 900
  • CD 為 400
  • XC 為 90
  • XL 為 40
  • IX 為 9
  • IV 為 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>

在以上示例中,使用者分別得到數字 577 和 3542 的羅馬數字為 DLXXVII 和 MMMDXLII。

示例 2

在下面的示例中,我們從使用者那裡獲取輸入,點選按鈕後,十進位制數字將轉換為羅馬數字。

<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>

使用 JavaScript,使用者學習瞭如何將任何十進位制數字轉換為羅馬數字。使用者瞭解到,當數字超過特定範圍時,十進位制數字在羅馬中的表示方式會有所不同。在這種情況下,使用者必須在基本數字上新增一個橫線。

更新於: 2022年8月22日

666 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.