Convert numeric to roman numeric in php and jquery
PHP Function to convert descimal numeric to roman numeric
<?php
function getRomanNumeric($yournumber)
{
$n = intval($yournumber);
$res = '';
$roman_numeric = 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 ($roman_numeric as $roman => $numeric)
{
$matches = intval($n / $numeric);
$res .= str_repeat($roman, $matches);
$n = $n % $numeric;
}
return $res;
}
echo getRomanNumeric(2); ////Ouput of convert numeric to roman numeric - II
?>
jQuery function to convert decimal numeric to roman numeric
function convertToRoman(num) {
var roman = "";
var lookupObj = {
1000:"M",
900:"CM",
500:"D",
400:"CD",
100:"C",
90:"XC",
50:"L",
40:"XL",
10:"X",
9:"IX",
4:"IV",
5:"V",
1:"I",
};
var arrayLen = Object.keys(lookupObj).length;
while(num>0){
for (i=arrayLen-1 ; i>=0 ; i--){
if(num >= Object.keys(lookupObj)[i]){
roman = roman + lookupObj[Object.keys(lookupObj)[i]];
num = num - Object.keys(lookupObj)[i];
break;
}
}
}
return roman;
}
Nice articel, This article help me very well. Thank you. Also please check my article on my site Know All About Htaccess Tutorial. In link article we will learn about Where is my htaccess file.
ReplyDelete