With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

Working with Roman Numerals

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.

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

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

See Also

The Encyclopaedia Brittanica article on “Mathematics, History Of “; the documentation with the Roman module; Section 6.23

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required