Site logo

Romanize-deromanize functions

Roman Numeral Converter

function Romanize(num) {
  var lookup = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},
      roman = '',
      i;
  for ( i in lookup ) {
    while ( num >= lookup[i] ) {
      roman += i;
      num -= lookup[i];
    }
  }
  return roman;
}
var romanized = romanize (1862);
var deromanized = deromanize ("MCMLXXVIII");

function romanize (num) {
	if (!+num)
		return false;
	var	digits = String(+num).split(""),
		key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
		       "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
		       "","I","II","III","IV","V","VI","VII","VIII","IX"],
		roman = "",
		i = 3;
	while (i--)
		roman = (key[+digits.pop() + (i * 10)] || "") + roman;
	return Array(+digits.join("") + 1).join("M") + roman;
}

function deromanize (str) {
	var	str = str.toUpperCase(),
		validator = /^M*(?:D?C{0,3}|C[MD])(?:L?X{0,3}|X[CL])(?:V?I{0,3}|I[XV])$/,
		token = /[MDLV]|C[MD]?|X[CL]?|I[XV]?/g,
		key = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},
		num = 0, m;
	if (!(str && validator.test(str)))
		return false;
	while (m = token.exec(str))
		num += key[m[0]];
	return num;
}

Arabic to roman by Kenshi Muto

function arabic2roman(s)
{
  var n = Number(s);
  var v = "";
  var i = ["", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"];
  var x = ["", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc"];
  var c = ["", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm"];
  var m = ["", "m", "mm", "mmm", "mmmm"];
  if (n >= 1 && n <= 4999) {
    v += m[Math.floor(n / 1000)];
    n %= 1000;
    v += c[Math.floor(n / 100)];
    n %= 100;
    v += x[Math.floor(n / 10)];
    n %= 10;
    v+= i[n];
  } 
  return v;
}

Roman to arabic by Kenshi Muto

function roman2arabic(s)
{
  var i = 0;
  var v = 0;
  var n, c;
  s = s.toLowerCase();
  if (s.charAt(i) == "m") {
    for (n = 0; s.charAt(i) == "m"; n++) i++;
    if (n > 4) return 0;
    v += n * 1000;
  }
  if (s.charAt(i) == "d" || s.charAt(i) == "c") {
    if ((c = s.charAt(i)) == "d") {
      i++;
      v += 500;
    }
    if (c == "c" && s.charAt(i + 1) == "m") {
      i += 2;
      v += 900;
    } else if (c == "c" && s.charAt(i + 1) == "d") {
      i += 2;
      v += 400;
    } else {
      for (n = 0; s.charAt(i) == "c"; n++) i++;
      if (n > 4) return 0;
      v += n * 100;
    }
  }
  if (s.charAt(i) == "l" || s.charAt(i) == "x") {
    if ((c = s.charAt(i)) == "l") {
      i++;
      v += 50;
    }
    if (c == "x" && s.charAt(i + 1) == "c") {
      i += 2;
      v += 90;
    } else if (c == "x" && s.charAt(i + 1) == "l") {
      i += 2;
      v += 40;
    } else {
      for (n = 0; s.charAt(i) == "x"; n++) i++;
      if (n > 4) return 0;
      v += n * 10;
    }
  }
  if (s.charAt(i) == "v" || s.charAt(i) == "i") {
    if ((c = s.charAt(i)) == "v") {
      i++;
      v += 5;
    }
    if (c == "i" && s.charAt(i + 1) == "x") {
      i += 2;
      v += 9;
    } else if (c == "i" && s.charAt(i + 1) == "v") {
      i += 2;
      v += 4;
    } else {
      for (n = 0; s.charAt(i) == "i"; n++) i++;
      if (n > 4) return 0;
      v += n;
    }
  }
  return (s.charAt(i) == "" && v >= 1 && v <= 4999) ? v : 0;
}


Roman to arabic script based on the function above