Php decimal to Roman number Conversion

roman.thumbnail

In many cases, we , php programmers need Roman number display I II III IV etc.

Here is a simple algorithm for that .

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
}
?>
11 Comments

11 Responses to “Php decimal to Roman number Conversion”

  1. NasirJumani April 20, 2008 at 7:35 pm #

    Cute little trick, stumbled!

  2. alex May 26, 2008 at 3:50 am #

    Also doesn’t really work:
    9 will come out as viiii instead of ix.

  3. Sajith M.R May 26, 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

  4. alex May 26, 2008 at 3:06 pm #

    Oh yes, you’re right, good shout, didn’t read it properly first time.

  5. Djordje June 11, 2008 at 5:38 pm #

    “private” modifier is useless outside of a class

    http://www.free-circuit-diagrams.com

  6. JonsJava April 22, 2009 at 12:44 am #

    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

  7. Borellus June 28, 2009 at 4:11 pm #

    Nice little function there, I think I may try to use it at some point.

  8. Hello July 20, 2009 at 9:18 pm #

    What happens with 10000?

  9. rean January 5, 2010 at 11:06 am #

    nice code…

  10. rean January 5, 2010 at 11:08 am #

    what are the uses of this $lookup as $roman => $value…

Leave a Reply

More in php, php source code (100 of 111 articles)


Hello friends, If you have a smpp account you can simple send sms / unicode to any number using ...