-
-
Save sasajib/5f9ba6ce826209c7fa55 to your computer and use it in GitHub Desktop.
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
/** | |
* Convert a decimal to a fraction. | |
* | |
* @param number $decimal | |
* @return string | |
*/ | |
public static function decimal2fraction($decimal) | |
{ | |
$decimalBase = --$decimal; | |
$denomenator = 1; | |
do { | |
$denomenator++; | |
$decimal = $decimalBase * $denomenator; | |
} while (intval($decimal) != $decimal); | |
if ($decimal % $denomenator == 0) { | |
$decimal = $decimal / $denomenator; | |
$denomenator = $denomenator / $denomenator; | |
} | |
return $decimal . '/' . $denomenator; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment