# tags: roman, numbers, awk, code # # Arabic to Roman Number Conversion # Michael Sanders 2023 # https://busybox.neocities.org/notes/arabic-to-roman.txt # # example usage: echo 6 | awk -f arabic-to-roman.txt # # note: using standard Roman numerals, the largest number # that can be represented is 3,999 written as MMMCMXCIX function toRoman(n) { if (n !~ /^[0-9]+$/ || n < 1 || n > 3999) return "invalid input" split("I IV V IX X XL L XC C CD D CM M", roman) split("1 4 5 9 10 40 50 90 100 400 500 900 1000", arabic) # loop through array in reverse order building romanNum for (x = 13; x >= 1; x--) { while (n >= arabic[x]) { romanNum = romanNum roman[x] n -= arabic[x] } } return romanNum } { print toRoman($1) } # eof