Numbers to Roman Numbers with php

Let us talk about scripts, HTML, Perl, PHP, apache, etc.
User avatar
Stevyn
SysOp
Posts:1786
Joined:Mon Nov 09, 2009 10:03 am
Location:Japan
Contact:
Numbers to Roman Numbers with php

Post by Stevyn » Fri Oct 27, 2023 11:29 am

Numbers to Roman Numbers with php

https://stackoverflow.com/questions/149 ... s-with-php

Code: Select all

/**
 * @param int $number
 * @return string
 */
function numberToRomanRepresentation($number) {
    $map = 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);
    $returnValue = '';
    while ($number > 0) {
        foreach ($map as $roman => $int) {
            if($number >= $int) {
                $number -= $int;
                $returnValue .= $roman;
                break;
            }
        }
    }
    return $returnValue;
}
Contact me directly: Ironfeatherbooks (@) gmail.com

Image

Post Reply