home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


2.6. 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; Recipe 6.23


Previous: 2.5. Operating on a Series of Integers Perl Cookbook Next: 2.7. Generating Random Numbers
2.5. Operating on a Series of Integers Book Index 2.7. Generating Random Numbers

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.