Numbers_Roman

Numbers_Roman provides methods to convert Arabic numbers like 23 into Roman numerals like XXIII and back.

Note : Both class methods are static; you do not need to instantiate an object.

Converting Arabic to Roman Numerals

The static method toNumeral does exactly this. It can be used for numbers from 1 to 5 999 999. Using it to convert higher numbers works, but does not produce historically correct results.

The second parameter $uppercase determines if the letters should be UPPERCASE (default) or not. Parameter number 3 sets if HTML code for overscores shall be generated; this is necessary for numbers greater than 3999. If the parameter is set to false, letters will be prefixed with an underscore _.

Exemple 55-1. Converting Arabic numbers to Roman numerals


<?php
require_once 'Numbers/Roman.php';

echo Numbers_Roman::toNumeral(23);
//returns: XXIII

echo Numbers_Roman::toNumeral(23false);
//returns: xxiii
?>

Converting Roman Numerals to Arabic numbers

Using the static method toNumber you can convert Roman numerals like XLII to Arabic numbers like 42. Letters prefixed with an underscore represent numbers larger than 1000.

Tableau 55-1. Conversion Table: Roman letter to Arabic number

RomanArabic
I1
V5
X10
L50
C100
D500
M1000
S, _V5000
R, _X10 000
P, _L50 000
Q, _C100 000
O, _D500 000
N, _M1 000 000

Exemple 55-2. Converting Roman numerals to Arabic numbers


<?php
require_once 'Numbers/Roman.php';

echo Numbers_Roman::toNumber('XLII');
//returns: 42
?>