Free Java Script Codes
List All
Slide Show
Ad Display
Image Effect
Animated Image Effects
Form Validation
Color Picker Tool
Formatting Forms
Select All
Dynamic Form select
Dynamic Color
Calendar
Textbox Counter
Alphanumeric
Date & Time
Stop Watch script
Delete Repeated Values
Pagination
Random Generator
Animated Text
Cursor Position
Window / User Info
Security / Authenticate
Bookmark
Mouse Effects
Title Bar
Status Bar
Country List
Free Games
Calculators
Convert Roman Numerals to Arabic Numbers
About
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
a) Used to display the numeric arabic value of the given roman numerals.
b) Provides easy accessibility.
Code
Javascript Code
<!-- Script by hscripts.com -->
<!-- Copyright of HIOXINDIA -->
<!-- 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="http://www.hscripts.com" id="dum" style="text-decoration:none; color: #dadada;">©h</a>
</div></td></tr>
</table>
Release Date - 06-05-2010
Get free version without ©copyright link for just
5
For customization of this script or any script development, contact us at support@hscripts.com
Usage
a) Copy and paste the javascript code into your HTML page and make use of this.
b) Here, the function "convert_numeric()" is used to convert roman numerals to arabic numbers.
License
- The javascript (misspelled as java script) is given under GPL License
- i.e. Free use for those who use the codes as it is.
- Free, if your modification does not remove our copyright information and links.
- Click Here for detailed license information.
- You can purchase the script if your requirements does not meet our GPL License terms.