Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Php Programming Code Examples

Php > Algorithms Code Examples

Function to convert Arabic numbers into Roman Numerals

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
Function to convert Arabic numbers into Roman Numerals Here's a little function I put together to convert an Arabic number between 0 and 9,999 to a Roman Numeral. Initial testing went well, but it has not been fully tested yet. <? function to_roman($num) { // Function to convert an arabic number ($num) to a roman numeral. $num must be between 0 and 9,999 if ($num < 0 || $num > 9999) { return -1; } // out of range $r_ones = array(1=> "I", 2=>"II", 3=>"III", 4=>"IV", 5=>"V", 6=>"VI", 7=>"VII", 8=>"VIII", 9=>"IX"); $r_tens = array(1=> "X", 2=>"XX", 3=>"XXX", 4=>"XL", 5=>"L", 6=>"LX", 7=>"LXX", 8=>"LXXX", 9=>"XC"); $r_hund = array(1=> "C", 2=>"CC", 3=>"CCC", 4=>"CD", 5=>"D", 6=>"DC", 7=>"DCC", 8=>"DCCC", 9=>"CM"); $r_thou = array(1=> "M", 2=>"MM", 3=>"MMM", 4=>"MMMM", 5=>"MMMMM", 6=>"MMMMMM", 7=>"MMMMMMM", 8=>"MMMMMMMM", 9=>"MMMMMMMMM"); $ones = $num % 10; $tens = ($num - $ones) % 100; $hundreds = ($num - $tens - $ones) % 1000; $thou = ($num - $hundreds - $tens - $ones) % 10000; $tens = $tens / 10; $hundreds = $hundreds / 100; $thou = $thou / 1000; if ($thou) { $rnum .= $r_thou[$thou]; } if ($hundreds) { $rnum .= $r_hund[$hundreds]; } if ($tens) { $rnum .= $r_tens[$tens]; } if ($ones) { $rnum .= $r_ones[$ones]; } return $rnum; } // function to_roman($num) ?>