2.5. Working with Roman Numerals
2.5.1. Problem
You want to convert between
regular numbers and Roman numerals. You need to do this with items in
outlines, page numbers on a preface, and copyrights for movie
credits.
2.5.2. Solution
Use the Roman
module from CPAN:
use Roman;
$roman = roman($arabic); # convert to roman numerals
$arabic = arabic($roman) if isroman($roman);
# convert from roman numerals
2.5.3. Discussion
The Roman module
provides both Roman and roman
for converting Arabic ("normal") numbers to their Roman equivalents.
Roman produces uppercase letters, whereas
roman gives lowercase ones.The module only deals with Roman numbers from 1 to 3999, inclusive.
The Romans didn't represent negative numbers or zero, and 5000 (which
4000 is represented in terms of) uses a symbol outside the ASCII
character set.
use Roman;
$roman_fifteen = roman(15); # "xv"
print "Roman for fifteen is $roman_fifteen\n";
$arabic_fifteen = arabic($roman_fifteen);
print "Converted back, $roman_fifteen is $arabic_fifteen\n";
Roman for fifteen is xv
Converted back, xv is 15
Or to print the current year:
use Time::localtime;
use Roman;
printf "The year is now %s\n", Roman(1900 + localtime->year);
The year is now MMIII
Now, if you happen to have Unicode fonts available, you'll find that
code points U+2160 through U+2183 represent Roman numerals, including
those beyond the typical ASCII values.
use charnames ":full";print "2003 is
N{ROMAN NUMERAL ONE THOUSAND}" x 2,
"N{ROMAN NUMERAL THREE}\n";
2003 is



characters.Believe it or not, there's even a CPAN module that lets you use Roman
numerals in arithmetic.
use Math::Roman qw(roman);
print $a = roman('I'); # I
print $a += 2000; # MMI
print $a -= "III"; # MCMXCVIII
print $a -= "MCM"; # XCVIII
2.5.4. See Also
The Encyclopaedia Britannica article on "Mathematics, History Of";
the documentation with the CPAN modules Roman and Math::Roman; Recipe 6.23