Created
July 16, 2020 07:04
-
-
Save nurrachmat-nr/711f01aecad683efc1e8b520832a0ab1 to your computer and use it in GitHub Desktop.
Fungsi PHP untuk mengubah angka menjadi bilangan romawi
This file contains 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
/** | |
* @package njr_helper | |
* @subpackage Helper | |
* @author Nur Rachmat <rachmat.nur91@gmail.com> | |
* @version 0.1 | |
* @copyright Copyright © 2017 Nur Rachmat <rachmat.nur91@gmail.com> | |
*/ | |
/** | |
* Convert Number to Roman | |
* @param integer $integer | |
* @return string | |
*/ | |
function convertToRoman($integer) | |
{ | |
// Convert the integer into an integer (just to make sure) | |
$integer = intval($integer); | |
$result = ''; | |
// Create a lookup array that contains all of the Roman numerals. | |
$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($integer / $value); | |
// Add the same number of characters to the string | |
$result .= str_repeat($roman, $matches); | |
// Set the integer to be the remainder of the integer and the value | |
$integer = $integer % $value; | |
} | |
// The Roman numeral should be built, return it | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment