Convert Roman Numerals to Arabic Numbers

JavaScript used to convert given roman numerals to its equivalent arabic numbers. Simply enter the roman value in the appropriate space below and click on the Convert button.

Features

  • Used to display the numeric arabic value of the given roman numerals.
  • Provides easy accessibility.

Preview


Roman Number:
Numeric Value:
©h

Downloads

Javascript Code


<!-- Script by hscripts.com -->
<!-- More scripts @www.hscripts.com -->
<style type="text/css">
.txt{border:1px solid grey;width:150px;height:25px;font-size:12px;padding:5px;font-family:arial,verdana;}
.fnt{font-size:12px;font-family:arial,verdana;}
</style>
<script type="text/javascript">
var rarr = new Array("M",
"CM", "D", "CD", "CCC", "CC", "C", "XC", "L", "XL", "XXX", "XX", "X", "IX", "V", "IV", "III", "II", "I");var narr = new Array(1000,900,500,400,300,200,100,90,50,40,30,20,10,9,5,4,3,2,1); var warr = new Array("CMCM",
"CMD",
"CMCD", "CMC", "DD", "DCD", "CDCD", "CDC", "CCCC", "XCXC",
"XCL", "XCXL", "XCX", "LL", "LXL", "XLXL", "XLX", "XXXX", "IXIX",
"IXV", "IXIV", "IXI", "IVIV", "IVI", "IIII");var carr= new Array("MDCCC", "MCD",
"MIII", "M",
"M",
"CM",
"DCCC", "D",
"CD",
"CLXXX", "CXL", "CIII", "C",
"C",
"XC",
"LXXX", "L",
"XL",
"XVIII", "XIV", "XIII", "X",
"VIII", "V",
"IV");
function convert_numeric()
{var rom = document.getElementById("roman").value;var roman = rom.replace(/ /g, "");roman = roman.toUpperCase();roman = roman.replace(/[^IVXLCDM]/g, "");document.getElementById("roman").value=roman;if (roman.length == 0){document.getElementById("res").innerHTML = "";return;}var position = 0;var result = 0;var pp = -1;while(position < roman.length){var p = getnextletter(roman, position);if (pp != 0){if ( narr[pp] < narr[p] ){document.getElementById("res").innerHTML = "Error. "+rarr[p]+" should not follow "+rarr[pp];return;}}if(p < 0)return;position += rarr[p].length;result += narr[p];pp = p;}document.getElementById("res").innerHTML =
result;
}
function getnextletter(roman, position)
{for (i=0; i<warr.length; i++){if ( roman.indexOf(warr[i], position) == position ){document.getElementById("res").innerHTML = warr[i] + " is not correct replace with " + carr[i] + ".";return -1;}}for (i=0; i<rarr.length; i++){if ( roman.indexOf(rarr[i], position) == position)return i;}document.getElementById("res").innerHTML = "invalid at " +position;return -1;
}
</script><!-- Script by hscripts.com -->


HTML Code


<table>
<tr><td class="fnt">Roman Number:</td><td><input type="text"
class="txt" id="roman" value="" onkeyup="convert_numeric()"></td></tr>
<tr><td class="fnt">Numeric Value:</td><td class="fnt"><div id="res" style='border:1px solid grey;height:22px; padding: 2px; padding-left: 8px;'></div></td></tr>
<tr><td colspan=2><div align=center style="font-size: 10px; color:green;" id="dumdiv">
<a href="https://www.hscripts.com" id="dum" style="text-decoration:none; color: #dadada;">&copy;h</a>
</div></td></tr>
</table>


  • Release Date - 06-05-2010
  • Get free version without ©copyright link for just $10/-
  • For customization of this script or any script development, mail to support@hscripts.com

Usage

  • Copy and paste the javascript code into your HTML page and make use of this.
  • Here, the function "convert_numeric()" is used to convert roman numerals to arabic numbers.

License

Related Scripts

Free Javascript Scripts


Ask Questions

Ask Question