What is going on here???? 😕
i'm trying to write a function that will return the integer value + html entity for 1/4 1/2 and 3/4 when given a float value.
i wrote this simple test that loops from 0 to 2 in .05 increments, outputting everything in an ugly little table. BUT!! for some reason .5 always comes out as 1/4??? what did i miss?
<table border="2">
<tr><td>i</td><td>left</td><td>right</td><td>fraction</td></tr>
<?php
for($i=0; $i<2; $i=$i+(.05)){
$left = intval($i);
$right = $i - $left;
$frac = "&nbsp;";
if($right < 0.25){
$frac = "&nbsp;";
} elseif ($right < .5){
$frac = "&frac14;";
} elseif ($right < 0.75){
$frac = "&frac12;";
} else {
$frac = "&frac34;";
}
echo "<tr><td>$i</td><td>$left</td><td>$right</td><td>$frac</td></tr>";
}
?>
</table>
oh yea, and 1 minus 1 comes out as 2.22044604925E-16 !!! is this a problem with the intval function or more likely my inelegant coding?