Here's code to convert numbers from arabic to roman numerals (up to 5000, I was lazy and
ASCII doesn't have those funky characters with bars above them).
The code is, as surprising as it may sound, in Rexx. It was written by me today, in Rexx just for the heck of it. =)
I should have written this in some old IBM mainframe assembler to get the feel of archaicity... =)
/* Ideas from britannica.com - thanks to the wise ones there
I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000
This *could* have been implemented more... intelligently, but
that would certainly have been overkill.
*/
RomanNum: procedure; parse arg number
/* The Ancient Romans Were Not Y0 Compliant. */
if number <= 0 then return number /* No zero, no negative numbers */
if number % number != 0 then return number /* No decimal numbers */
if number > 5000 then return number /* No numbers above 5000 either -
limitation of ASCII, mostly */
number = right(number,4,'0') /* Pad with zeroes */
nthousands = value(substr(number,1,1)) /* Split apart per number */
nhundreds = value(substr(number,2,1))
ntens = value(substr(number,3,1))
nones = value(substr(number,4,1))
select
when nones = 1 then cones = 'I'
when nones = 2 then cones = 'II'
when nones = 3 then cones = 'III'
when nones = 4 then cones = 'IV'
when nones = 5 then cones = 'V'
when nones = 6 then cones = 'VI'
when nones = 7 then cones = 'VII'
when nones = 8 then cones = 'VIII'
when nones = 9 then cones = 'IX'
when nones = 0 then cones = ''
end
select
when ntens = 1 then ctens = 'X'
when ntens = 2 then ctens = 'XX'
when ntens = 3 then ctens = 'XXX'
when ntens = 4 then ctens = 'XL'
when ntens = 5 then ctens = 'L'
when ntens = 6 then ctens = 'LX'
when ntens = 7 then ctens = 'LXX'
when ntens = 8 then ctens = 'LXXX'
when ntens = 9 then ctens = 'XC'
when ntens = 0 then ctens = ''
end
select
when nhundreds = 1 then chundreds = 'C'
when nhundreds = 2 then chundreds = 'CC'
when nhundreds = 3 then chundreds = 'CCC'
when nhundreds = 4 then chundreds = 'CD'
when nhundreds = 5 then chundreds = 'D'
when nhundreds = 6 then chundreds = 'DC'
when nhundreds = 7 then chundreds = 'DCC'
when nhundreds = 8 then chundreds = 'DCCC'
when nhundreds = 9 then chundreds = 'CM' /* M's appended */
when nhundreds = 0 then chundreds = ''
end
cthousands = copies('M',nthousands)
result = cthousands || chundreds || ctens || cones
return result
Tested under Linux with Regina Rexx interpreter.