flaschenkind
Well-known member
- 20 April 2006
- 4.507
- 337
Ich hab mal ne Funktion geschrieben, mit der man eine Dezimale Zahl in eine Römische umwandeln kann.
PHP:
<?php
/*
Konvertiert eine Dezimalzahl in eine Römische Zahl
@author MF-Scripts
@param Integer $number
@return Integer, wenn $number <= 3999, sonst Boolean false
*/
function decimal2roman($number){
// jeder Buchstabe darf maximal dreimal hintereinander vorkommen, somit ist die größte Zahl 3999 (MMMCMXCIX)
if($number > 3999){
return false;
}
$roman_number = '';
$potenz = 1000;
while($number > 0){
$number_tmp = floor($number/$potenz);
$number %= $potenz;
if($number_tmp == 0)
$potenz /= 10;
if($potenz == 1){
if($number_tmp == 1)
$roman_number .= 'I';
elseif($number_tmp == 2)
$roman_number .= 'II';
elseif($number_tmp == 3)
$roman_number .= 'III';
elseif($number_tmp == 4)
$roman_number .= 'IV';
elseif($number_tmp == 5)
$roman_number .= 'V';
elseif($number_tmp == 6)
$roman_number .= 'VI';
elseif($number_tmp == 7)
$roman_number .= 'VII';
elseif($number_tmp == 8)
$roman_number .= 'VIII';
elseif($number_tmp == 9)
$roman_number .= 'IX';
}
if($potenz == 10){
if($number_tmp == 1)
$roman_number .= 'X';
elseif($number_tmp == 2)
$roman_number .= 'XX';
elseif($number_tmp == 3)
$roman_number .= 'XXX';
elseif($number_tmp == 4)
$roman_number .= 'XL';
elseif($number_tmp == 5)
$roman_number .= 'L';
elseif($number_tmp == 6)
$roman_number .= 'LX';
elseif($number_tmp == 7)
$roman_number .= 'LXX';
elseif($number_tmp == 8)
$roman_number .= 'LXXX';
elseif($number_tmp == 9)
$roman_number .= 'XC';
}
if($potenz == 100){
if($number_tmp == 1)
$roman_number .= 'C';
elseif($number_tmp == 2)
$roman_number .= 'CC';
elseif($number_tmp == 3)
$roman_number .= 'CCC';
elseif($number_tmp == 4)
$roman_number .= 'CD';
elseif($number_tmp == 5)
$roman_number .= 'D';
elseif($number_tmp == 6)
$roman_number .= 'DC';
elseif($number_tmp == 7)
$roman_number .= 'DCC';
elseif($number_tmp == 8)
$roman_number .= 'DCCC';
elseif($number_tmp == 9)
$roman_number .= 'CM';
}
if($potenz == 1000){
if($number_tmp == 1)
$roman_number .= 'M';
elseif($number_tmp == 2)
$roman_number .= 'MM';
elseif($number_tmp == 3)
$roman_number .= 'MMM';
}
}
return $roman_number;
}
echo decimal2roman(3999);
?>
Zuletzt bearbeitet: