Freeware JavaScript Editor Perl Tutorials

↑

Main Page |

## Recipe 2.5 Working with Roman Numerals## 2.5.1 ProblemYou 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 SolutionUse 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 DiscussionThe Roman module
provides both 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"; Or to print the current year: use Time::localtime; use Roman; printf "The year is now %s\n", Roman(1900 + localtime->year); 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";
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 ## 2.5.4 See AlsoThe Encyclopaedia Britannica article on "Mathematics, History Of"; the documentation with the CPAN modules Roman and Math::Roman; Recipe 6.23 |

Main Page |

↓

JavaScript Verifier Perl Tutorials

©