This script is the way how to convert number (1,2,3,4) to roman number ( i,ii,iii,iv).
Basic roman number:
<?
function numberToRoman($num,$type){
if($type == 1){ //upper character number
// Make sure that we only use the integer portion of the value
$n = intval($num);
$result = '';
// Declare a lookup array that we will use to traverse the number:
$lookup = 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 ($lookup as $roman => $value){
// Determine the number of matches
$matches = intval($n / $value);
// Store that many characters
$result .= str_repeat($roman, $matches);
// Substract that from the number
$n = $n % $value;
}
// The Roman numeral should be built, return it
return $result;
}
elseif($type == 2){ //low character number
// Make sure that we only use the integer portion of the value
$n = intval($num);
$result = '';
// Declare a lookup array that we will use to traverse the number:
$lookup = 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 ($lookup as $roman => $value){
// Determine the number of matches
$matches = intval($n / $value);
// Store that many characters
$result .= str_repeat($roman, $matches);
// Substract that from the number
$n = $n % $value;
}
// The Roman numeral should be built, return it
return $result;
}
}
// and your condition to call them (use function )
for($i=1;$i<1000;$i++){
echo numberToRoman($i,1)."<br>";
}
?>
modify from - go4expert.com
No comments:
Post a Comment