# Recipe 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

However, the Roman module doesn't yet have an option to use those 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```

The Encyclopaedia Britannica article on "Mathematics, History Of"; the documentation with the CPAN modules Roman and Math::Roman; Recipe 6.23

 Chapter 1. Strings
 Chapter 3. Dates and Times
 Chapter 4. Arrays
 Chapter 5. Hashes
 Chapter 6. Pattern Matching
 Chapter 7. File Access
 Chapter 8. File Contents
 Chapter 9. Directories
 Chapter 10. Subroutines
 Chapter 11. References and Records
 Chapter 12. Packages, Libraries, and Modules
 Chapter 13. Classes, Objects, and Ties
 Chapter 14. Database Access
 Chapter 15. Interactivity
 Chapter 16. Process Management and Communication
 Chapter 17. Sockets
 Chapter 18. Internet Services
 Chapter 19. CGI Programming
 Chapter 20. Web Automation
 Chapter 21. mod_perl
 Chapter 22. XML