## Posted By

TopcatSoftware on 01/09/11

# arabic2roman()

/ Published in: Awk

Simple arabic to roman numeral conversion in awk

`function arabic2roman(x,    y) {# arabic to roman numeral conversion in awk# Topcat Software LLC. - 2010# http://www.topcat.hypermart.net/index.html# example: foo = arabic2roman(100)   if (!x) {return "nulla"}  if (x > 3999) {return "max input: 3999"}   while (x >= 1000) {x -= 1000; y = y "M" }  while (x >=  900) {x -=  900; y = y "CM"}  while (x >=  500) {x -=  500; y = y "D" }  while (x >=  400) {x -=  400; y = y "CD"}  while (x >=  100) {x -=  100; y = y "C" }  while (x >=   90) {x -=   90; y = y "XC"}  while (x >=   50) {x -=   50; y = y "L" }  while (x >=   40) {x -=   40; y = y "XL"}  while (x >=   10) {x -=   10; y = y "X" }  while (x >=    9) {x -=    9; y = y "IX"}  while (x >=    5) {x -=    5; y = y "V" }  while (x >=    4) {x -=    4; y = y "IV"}  while (x >=    1) {x -=    1; y = y "I" }   return y } # a 2nd example (more succinct) # awk script converts arabic numerals to roman numerals# usage example: 'awk -f arabic2roman.awk 100' BEGIN {   split("1000 900 500 400 100 90 50 40 10 9 5 4 1", ar)  split("M CM D CD C XC L XL X IX V IV I", ro)   print arabic2roman(ARGV[1]) } function arabic2roman(x,      y, z) {   if (!x) {return "nulla"}  if (x > 3999) {return "max input: 3999"}   for (y=1; ar[y]; y++) {while (x >= ar[y]) {x -= ar[y]; z = z ro[y]}}   return  z }`