Tuesday 10th July 2007

by Sajith M.R

roman.thumbnail

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;
}

?>
· · · ◊ ◊ ◊ · · ·

11 Responses to “Php decimal to Roman number Conversion”

  1. rean says:

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

  2. Hello says:

    What happens with 10000?

  3. Borellus says:

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

  4. JonsJava says:

    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

  5. Djordje says:

    “private” modifier is useless outside of a class

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

  6. alex says:

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

  7. Sajith M.R says:

    @Alex
    That might be your mistake. I tried 9 and i got IX as output.
    The function is right.

    Regards
    Sajith

  8. alex says:

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

  9. NasirJumani says:

    Cute little trick, stumbled!

· · · ◊ ◊ ◊ · · ·

Leave a Reply

وظائف 2011 تحويل العملات برامج برنامج تسريع التحميل برنامج الفلاش برنامج محول الصوتيات عربي hotel 2011 زيادة رواتب العساكر 1431