Created
August 1, 2020 15:45
-
-
Save MakStashkevich/5f73ce951f166578e4c6ad14a3f539ba to your computer and use it in GitHub Desktop.
Convert all numbers to Roman style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function getRomanNumeral(int $integer): string | |
{ | |
$romanNumeralConversionTable = [ | |
'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 | |
]; | |
$romanString = ""; | |
while ($integer > 0) { | |
foreach ($romanNumeralConversionTable as $rom => $arb) { | |
if ($integer >= $arb) { | |
$integer -= $arb; | |
$romanString .= $rom; | |
break; | |
} | |
} | |
} | |
return $romanString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment