/**
* 获取两个数的最大公约数
* @param $a
* @param $b
* @return mixed
*/
public function fnCommonDivisor($a,$b){
while($a != $b){
if($a > $b){
$a -= $b;
}else{
$b -= $a;
}
}
return $a;
}
/**
*
* 获取最简真分数
* @param $grade //分数 100/1000
* @return string //最简真分数 1/10
*/
public function fnFractionInLowestTerm($grade) {
if (empty($grade)) return ' ';
if (strpos($grade, '/') === false) return $grade.'s';
$arr = explode('/', $grade);
$commonDivisor = $this->fnCommonDivisor($arr[0], $arr[1]);
if ( empty($arr[0]) || empty($arr[1]) ) return 0;
$molecule = $arr[0]/$commonDivisor;
$denominator = $arr[1]/$commonDivisor;
$grade = $molecule .'/'.$denominator.'s';
return $grade;
}
/**
* 案例:fnFractionalConversion(0.25) return 1/4
* @param $val //小数值转化成分数
* @param float $threshold //小数
* @return string|void
*/
public function fnFractionalConversion($val, $threshold=0.001 ) {
$i=1;
$j =1;
while (abs(($i /$j) - $val ) > $threshold ) {
if ( $i/$j > $val ) {
$j ++ ;
} else if ( ($i/$j) < $val ) {
$i ++ ;
}
}
return $i .'/'.$j;
}