Home

 

 

 

JavaScript
Roman Numeral Year Converter
(To 9999)

Decimal:
Roman Numeral:
 

The following code is "pennance" for having completely forgot on a mailing list that the 21st century does not technically begin on Jan 1, 2000, since the roman numerals by which it is counted lack a 0 placeholder, which screws up the calculations. So, rather than admit the errors of my ways, I simply asserted that "in issues of numerology, I am a complete Arabic chauvinist." In any case, I sometimes practice programming, and this is another one of my useless exercises.

Code


/* JavaScript by Peter Kosenko, peter1253@yahoo.com, http://home.netwood.net/kosenko/
 * Feel free to use an adapt it, if you have any use for it (which I seriously doubt).
 * But try to remember to give me credit.  The IsInteger() function comes from the standard
 * Netscape JavaScript form validation set.
 */

var digitArray = new Array; // to store the decimal digits

/* Number digit positions are from right to left
 *
 *	number                  777
 *	digit array index       210
 *
 * Since we will be using 0-based array positions to evaluate,
 * eval("pos" + pos + "Array[i]") will return an appropriate roman
 * string (except, of course, after position 2)
 */

var pos3Array = new Array;
pos3Array[0] = "M";  // only one character required for thousands

var pos2Array = new Array;
pos2Array[0] = "C"; pos2Array[1] = "CD"; pos2Array[2] = "D"; pos2Array[3] = "CM";

var pos1Array = new Array;
pos1Array[0] = "X"; pos1Array[1] = "XL"; pos1Array[2] = "L"; pos1Array[3] = "XC";

var pos0Array = new Array;
pos0Array[0] = "I"; pos0Array[1] = "IV"; pos0Array[2] = "V"; pos0Array[3] = "IX";

function IsInteger(s) {
	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!((c >= "0") && (c <= "9"))) {
			return false;
		}
	}
	return true;
}

function remainder(pos, numChars) {
	var string = "";
	for (var i = 1; i <= numChars; i++) {
		string += eval("pos" + pos + "Array[0]");
	}
	return string;
}

function convertDigit(pos, digit) {

	digit = parseInt(digit); // convert to integer
	if (pos <= 2) {
		if (digit == 0) return "";
		if (digit == 9) return eval("pos" + pos + "Array[3]");
		if (digit == 5) return eval("pos" + pos + "Array[2]");
		if (digit == 4) return eval("pos" + pos + "Array[1]");
		if (digit > 5) return (eval("pos" + pos + "Array[2]")	+ remainder(pos, digit - 5));
		return remainder(pos, digit);
	} else {
		return remainder(pos, digit);
	}
}

function processForm(Form) {

	var i, j;
	var decimal = Form.decimal.value;
	var roman = "";

	if (!IsInteger(decimal)) {
		alert("Hey, your input contains characters other than digits 0 to 9.\n" +
		"You'll have to correct the error for the program to work.");
	} else {

		// store decimal digits in array
	
		for (i = 0; i < decimal.length ; i++) {
			digitArray[i] = decimal.charAt(i);
		}
	
		// read out digits backwards, converting and adding to roman
		// j counts forward from 0 to give number position from right to left (0,1,2)
	
		for (i = decimal.length - 1, j = 0; i >= 0; i--, j++) {
			roman = convertDigit(j, digitArray[i]) + roman;
		}
		j = 0; // reset

	}

	// place roman value in roman textbox

	Form.roman.value = roman;
}

The Form



<form name="form1" method="POST">

<table
  border="1" width="80%">
    <tr>
      <td width="50%" valign="top">Decimal:</td>
      <td width="50%" valign="top"><input type="text" name="decimal" size="4" maxlength="4"></td>
    </tr>
    <tr>
      <td width="50%">Roman Numeral:</td>
      <td width="50%"><input type="text" name="roman" size="40"></td>
    </tr>
    <tr>
      <td width="50%"> </td>
      <td width="50%"><input type="button" value="Convert" name="convert" onClick="processForm(form1)">
                                <input type="reset" value="Reset" name="reset"></td>
    </tr>
  </table>
</form>


| Writing | Web Design | Programming | Music | Links |


Home Home  Home

E-mail Pete the answers to all his questions.