Convert Numbers To Roman Numerals
Roman numerals can look very stately on a page or as numbers on a clock, giving an olde worlde look to a site, or for formal presentations. This simple function will convert normal numbers into roman numerals.
<?php
/**
*
* @create a roman numeral from a number
*
* @param int $num
*
* @return string
*
*/function romanNumerals($num)
{
$n = intval($num);
$res = '';
/*** roman_numerals array ***/
$roman_numerals = array(
'M' => 1000,
'CM' => 900,
'D' => 500,
'CD' => 400,
'C' => 100,
'XC' => 90,
'L' => 50,
'XL' => 40,
'X' => 10,
'IX' => 9,
'V' => 5,
'IV' => 4,
'I' => 1);
foreach ($roman_numerals as $roman => $number)
{
/*** divide to get matches ***/
$matches = intval($n / $number);
/*** assign the roman char * $matches ***/
$res .= str_repeat($roman, $matches);
/*** substract from the number ***/
$n = $n % $number;
}
/*** return the res ***/
return $res;
}?>
<?php
/*** use the current year as the number to convert ***/
$year = date('Y');
/*** echo the roman numeral for the year ***/
echo romanNumerals($year);?>
Hello Friends! I am Ramana a part time blogger from Hyderabad.
0 comments:
Post a Comment