Php decimal to Roman number Conversion
Posted on 10. Jul, 2007 by Sajith M.R in php, php source code
![]()
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;
}
?>



Geoserv
Apr 18th, 2008
STUMBLED!
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
Apr 20th, 2008
Cute little trick, stumbled!
alex
May 26th, 2008
Also doesn’t really work:
9 will come out as viiii instead of ix.
Sajith M.R
May 26th, 2008
@Alex
That might be your mistake. I tried 9 and i got IX as output.
The function is right.
Regards
Sajith
alex
May 26th, 2008
Oh yes, you’re right, good shout, didn’t read it properly first time.
Djordje
Jun 11th, 2008
“private” modifier is useless outside of a class
http://www.free-circuit-diagrams.com
JonsJava
Apr 22nd, 2009
Nice to see you steal stuff from other websites. Too bad you forgot to copy and paste the whole thing.
http://www.go4expert.com/forums/showthread.php?t=4948
Borellus
Jun 28th, 2009
Nice little function there, I think I may try to use it at some point.
Hello
Jul 20th, 2009
What happens with 10000?
rean
Jan 5th, 2010
nice code…
rean
Jan 5th, 2010
what are the uses of this $lookup as $roman => $value…