Tuesday 10th July 2007
by Sajith M.R![]()
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;
}
?>
what are the uses of this $lookup as $roman => $value…
nice code…
What happens with 10000?
Nice little function there, I think I may try to use it at some point.
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
“private” modifier is useless outside of a class
http://www.free-circuit-diagrams.com
Oh yes, you’re right, good shout, didn’t read it properly first time.
@Alex
That might be your mistake. I tried 9 and i got IX as output.
The function is right.
Regards
Sajith
Also doesn’t really work:
9 will come out as viiii instead of ix.
Cute little trick, stumbled!
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/