Integer to roman numeral conversion - Php

Integer_roman_num

Snippet Code


  
Rate this page :
  [ 0 votes]

This function is used to convert an integer to a Roman numeral

<?php function roman($num){ $n = intval($num); $res = ''; $romanNumerals = 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 ($romanNumerals as $roman => $number){ $matches = intval($n / $number); $res .= str_repeat($roman, $matches); $n = $n % $number; } return $res; } echo roman(23); ?>

Tags


Ask Questions

Ask Question