Ehi, cosi?
Codice PHP:
$numero = 1.5;
$rapporto = array(
"numeratore" => 0,
"denominatore" => 1,
);
//Creo la frazione decimale
$rapporto["numeratore"] = $numero;
while ($rapporto["numeratore"] !== round($rapporto["numeratore"])) {
$rapporto["numeratore"] *= 10;
$rapporto["denominatore"] *= 10;
}
//MCD con l'algoritmo di Euclide
$tmp1 = $rapporto["numeratore"];
$tmp2 = $rapporto["denominatore"];
while ($tmp1 > 0) {
if ($tmp1 < $tmp2) {
$tmp3 = $tmp1;
$tmp1 = $tmp2;
$tmp2 = $tmp3;
}
$tmp1 = $tmp1 - $tmp2;
}
$MCD = $tmp2;
//semplifico
$rapporto["numeratore"] /= $MCD;
$rapporto["denominatore"] /= $MCD;
print_r($rapporto);