Php decimal to Roman number Conversion
![]()
In many cases, we , php programmers need Roman number display I II III IV etc.
Here is a simple algorithm for that .
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;
}
?>
|
|
|
|
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Stumble Reviews
Plugin powered by StumbleCult6 Responses to “Php decimal to Roman number Conversion”
-
Geoserv Says:
April 18th, 2008 at 11:57 pmSTUMBLED!
Sweet, good post.
VOTED for you at:
http://www.newsdots.com/tutorials/php-decimal-to-roman-number-conversion-programming-ideas-logics-tips-and-tricks-sajithmr-com/ -
NasirJumani Says:
April 20th, 2008 at 7:35 pmCute little trick, stumbled!
-
alex Says:
May 26th, 2008 at 3:50 amAlso doesn’t really work:
9 will come out as viiii instead of ix. -
Sajith M.R Says:
May 26th, 2008 at 12:39 pm@Alex
That might be your mistake. I tried 9 and i got IX as output.
The function is right.Regards
Sajith -
alex Says:
May 26th, 2008 at 3:06 pmOh yes, you’re right, good shout, didn’t read it properly first time.
-
Djordje Says:
June 11th, 2008 at 5:38 pm“private” modifier is useless outside of a class


Review on — May 11, 2008, 3:27 pm
Review on — May 10, 2008, 12:14 pm
Review on — May 4, 2008, 10:15 am
Review on — April 28, 2008, 5:46 pm
Review on — April 19, 2008, 3:57 am
Review on — April 17, 2008, 3:12 pm
Review on — May 25, 2008, 6:23 pm