Integer to Roman numeral

Changes and integer to a roman numeral.

	private function numberToRoman($num)
	{
	     $n = intval($num);
	     $result = '';

	     $lookup = array('M' => 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)
	     {
	            $matches = intval($n / $value);
	            $result .= str_repeat($roman, $matches);
	            $n = $n % $value;
	      }

	      return $result;
	}

Originally found here



Comments

No comments yet.

Add Yours

  • Author Avatar

    YOU


Comment Arrow



About Author

Sirbastian

Probably the best web developer in the whole world.