Numbers To Roman
This algorithm converts decimal numbers to roman numerals.
function NumbersToRoman($num)
{
$roman = "";
$totalM = floor($num / 1000);
$num %= 1000;
$totalCM = floor($num / 900);
$num %= 900;
$totalD = floor($num / 500);
$num %= 500;
$totalCD = floor($num / 400);
$num %= 400;
$totalC = floor($num / 100);
$num %= 100;
$totalXC = floor($num / 90);
$num %= 90;
$totalL = floor($num / 50);
$num %= 50;
$totalXL = floor($num / 40);
$num %= 40;
$totalX = floor($num / 10);
$num %= 10;
$totalIX = floor($num / 9);
$num %= 9;
$totalV = floor($num / 5);
$num %= 5;
$totalIV = floor($num / 4);
$num %= 4;
$roman .= RepeatString("M", $totalM);
$roman .= RepeatString("CM", $totalCM);
$roman .= RepeatString("D", $totalD);
$roman .= RepeatString("CD", $totalCD);
$roman .= RepeatString("C", $totalC);
$roman .= RepeatString("XC", $totalXC);
$roman .= RepeatString("L", $totalL);
$roman .= RepeatString("XL", $totalXL);
$roman .= RepeatString("X", $totalX);
$roman .= RepeatString("IX", $totalIX);
$roman .= RepeatString("V", $totalV);
$roman .= RepeatString("IV", $totalIV);
$roman .= RepeatString("I", $num);
return $roman;
}
function RepeatString($s, $count)
{
if ($count < 0) return "";
$str = "";
for ($i = 0; $i < $count; $i++)
{
$str .= $s;
}
return $str;
}
Example
$data = 419;
$value = NumbersToRoman($data);
Output
CDXIX