Last active
December 23, 2015 00:09
-
-
Save jimjam88/6551257 to your computer and use it in GitHub Desktop.
Convert a decimal to a fraction.
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