Jul 10 2007

Php decimal to Roman number Conversion

Decimal to Roman Number

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

?>

TAGS:

6 Comments on this post

Trackbacks

  1. Geoserv said:
  2. NasirJumani said:

    Cute little trick, stumbled!

    April 20th, 2008 at 7:35 pm
  3. alex said:

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

    May 26th, 2008 at 3:50 am
  4. Sajith M.R said:

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

    Regards
    Sajith

    May 26th, 2008 at 12:39 pm
  5. alex said:

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

    May 26th, 2008 at 3:06 pm
  6. Djordje said:

    “private” modifier is useless outside of a class

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

    June 11th, 2008 at 5:38 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

    Advt on sajithmr.com
    Advt on sajithmr.com
    Advt on sajithmr.com
    Itslife Online

Recent Comments

  • sexasion: Best of the Best sexypicturess.blogspot.com
  • memo ezzo: live sex hot video
  • muhammadashraf44: i am ashraf from karachi i have only one girl in sex and one night plzz
  • Arun Basil Lal: Dude, The comments to this post are ever flowing in, I am gonna post some hot pics on my blog too.....
  • badar: plz send me the hot pic

Recent Readers

JOIN MY COMMUNITY!

Recent Posts