Thursday, October 15, 2015

Decimal To Roman Numeral in JQuery

A program that I wrote using JQuery that will ask the user to enter a decimal number and then it will convert into a roman numeral format. The code is very simple and easy to understand JQuery is one of the most popular Javascript framework library that is widely used by many web developers around the world.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.


Program Listing

<html>
<head>
<script type="text/javascript" language="Javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
 </head>
 <style>
 h3 {
  font-family:arial;
  color:green;
  }
  </style>
<body>
<script language="javascript" type="text/javascript">
function  convert_to_roman(val) 

{
var roman_numerals=['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'];
var list_numbers=[1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
var x = parseInt(val);
if( x<1 || x>4999 ) 
{
alert(x+' number is out of range.');
   return 0;
}
display_result = '';
for(a=12; a>=0;)
if( list_numbers[a]<=x ) 
{
display_result += roman_numerals[a];
x -= list_numbers[a];
}
else
{
a--;
}
return(display_result);
}

$(document).ready(function () {
  value_number = parseInt(prompt("Enter a number"));
  document.write("<h3>"+value_number + " is equivalent to " +
  convert_to_roman(value_number) + " in roman numerals. </h3>");
  });
</script>
 The input must be in the range of 1 - 4999, or I to MMMMCMXCIX.
</body>
</html>

1 comment: