2.8 Converting Between Arabic and Roman Numerals


You need to switch between regular Arabic numerals and Roman numerals.

Technique

Use PEAR's Numbers_Roman class to convert back and forth:

 <?php $number = 43; $numeral = Numbers_Roman::toRoman ($number); $number  = Numbers_Roman::toNumber ($numeral); print "$number in Roman Numerals is $numeral"; ?> 

Comments

The Numbers_Roman module works only with Roman numerals greater than 0 up to and including 3,999 (0 < x <= 3,999). This is because Romans didn't deal with negative numbers and zero, and 5000 (which is needed to display 4,000) uses a symbol that is not in the ASCII character set.

The Numbers_Roman module also provides an isRoman() method, which will return true if the first argument is a valid Roman numeral, and will return false otherwise .



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net