Source for file Roman.php
Documentation is available at Roman.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors : David Costa <gurugeek@php.net> |
// | Sterling Hughes <sterling@php.net> |
// +----------------------------------------------------------------------+
// $Id: Roman.php,v 1.14 2004/04/28 13:13:08 danielc Exp $
* Provides utilities to convert roman numerals to
* arabic numbers and convert arabic numbers to roman numerals.
* Supports lower case input and output and some furthers conversion
* @author David Costa <gurugeek@php.net>
* @author Sterling Hughes <sterling@php.net>
* Converts a roman numeral to a number
* @param string $roman The roman numeral to convert
* lower cased numerals are converted into
* @return integer $num The number corresponding to the
* Replacing the Numerals representing an integer higher then 4000
* e.g. _X represent 10 000 _L represent 50 000 etc
* we first convert them into single characters
array ('letter' => 'I', 'number' => 1 ),
array ('letter' => 'V', 'number' => 5 ),
array ('letter' => 'X', 'number' => 10 ),
array ('letter' => 'L', 'number' => 50 ),
array ('letter' => 'C', 'number' => 100 ),
array ('letter' => 'D', 'number' => 500 ),
array ('letter' => 'M', 'number' => 1000 ),
array ('letter' => 'S', 'number' => 5000 ),
array ('letter' => 'R', 'number' => 10000 ),
array ('letter' => 'Q', 'number' => 50000 ),
array ('letter' => 'P', 'number' => 100000 ),
array ('letter' => 'O', 'number' => 500000 ),
array ('letter' => 'N', 'number' => 1000000 ),
array ('letter' => 0 , 'number' => 0 )
while ($conv[$i]['number'] > 0 ) {
if (strtoupper($roman[$sidx]) == $conv[$i]['letter']) {
if ($state > $conv[$i]['number']) {
$arabic -= $conv[$i]['number'];
$arabic += $conv[$i]['number'];
$state = $conv[$i]['number'];
* A backwards compatibility alias for toNumeral()
function toRoman ($num, $uppercase = true )
* Converts a number to its roman numeral representation
* @param integer $num An integer between 0 and 3999
* inclusive that should be converted
* to a roman numeral integers higher than
* 3999 are supported from version 0.1.2
* For an accurate result the integer shouldn't be higher
* than 5 999 999. Higher integers are still converted but
* they do not reflect an historically correct Roman Numeral.
* @param bool $uppercase Uppercase output: default true
* @param bool $html Enable html overscore required for
* integers over 3999. default true
* @return string $roman The corresponding roman numeral
function toNumeral($num, $uppercase = true , $html = true )
$conv = array (10 => array ('X', 'C', 'M'),
5 => array ('V', 'L', 'D'),
1 => array ('I', 'X', 'C'));
$digit = (int) ($num / 1000 );
for ($i = 2; $i >= 0; $i-- ) {
$digit = (int) ($num / $power);
if (($digit == 9 ) || ($digit == 4 )) {
$roman .= $conv[1 ][$i] . $conv[$digit+1 ][$i];
* Preparing the conversion of big integers over 3999.
* One of the systems used by the Romans to represent 4000 and
* bigger numbers was to add an overscore on the numerals.
* Because of the non ansi equivalent if the html output option
* is true we will return the overline in the html code if false
* we will return a _ to represent the overscore to convert from
* numeral to arabic we will always expect the _ as a
* representation of the html overscore.
$over = '<span style="text-decoration:overline;">';
} elseif ($html == false ) {
* Replacing the previously produced multiple MM with the
* relevant numeral e.g. for 1 000 000 the roman numeral is _M
* (overscore on the M) for 900 000 is _C_M (overscore on both
* the C and the M) We initially set the replace to AFS which
* will be later replaced with the M.
* 500 000 is _D (overscore D) in Roman Numeral
* 400 000 is _C_D (overscore on both C and D) in Roman Numeral
* 100 000 is _C (overscore C) in Roman Numeral
* 90 000 is _X_C (overscore on both X and C) in Roman Numeral
* 50 000 is _L (overscore L) in Roman Numeral
* 40 000 is _X_L (overscore on both X and L) in Roman Numeral
* 10 000 is _X (overscore X) in Roman Numeral
* 5 000 is _V (overscore V) in Roman Numeral
* 4 000 is M _V (overscore on the V only) in Roman Numeral
* For an accurate result the integer shouldn't be higher then
* 5 999 999. Higher integers are still converted but they do not
* reflect an historically correct Roman Numeral.
$over. 'AFS'. $overe, $roman);
$over. 'C'. $overe. $over. 'AFS'. $overe, $roman);
$over. 'D'. $overe, $roman);
$over. 'C'. $overe. $over. 'D'. $overe, $roman);
$over. 'C'. $overe, $roman);
$over. 'X'. $overe. $over. 'C'. $overe, $roman);
$over. 'L'. $overe, $roman);
$over. 'X'. $overe. $over. 'L'. $overe, $roman);
$over. 'X'. $overe, $roman);
$over. 'V'. $overe, $roman);
'M'. $over. 'V'. $overe, $roman);
* Replacing AFS with M used in both 1 000 000
* Checking for lowercase output
if ($uppercase == false ) {
Documentation generated on Mon, 11 Mar 2019 10:16:08 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|