more on portfolio









January 2009
M T W T F S S
« May «-»  
 1234
567891011
12131415161718
19202122232425
262728293031  






 


Convert to Roman numbers in PHP

Log entry of May 4th 2008 05:51:24 pm
Filed under: PHP, Snippets
« BlogSticker: Spread and Use Firefox
Display Roman Numbers in CSS »

Here’s a nifty coding which I’ve found on the net on displaying Roman number like I II III IV etc.

<?php

// A function to return the Roman Numeral, given an integer
private function numberToRoman($num)
{
// Make sure that we only use the integer portion of the value
$n = intval($num);
$result = '';

// Declare a lookup array that we will use to traverse the number:
$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)
{
// 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;
}

?> 


Source: http://www.sajithmr.com/php-decimal-to-roman-number-conversion/






Leave a Reply