I have the following function that I wrote
function range_gen($num1,$num2){
$exp = explode('.',$num1);
$accuracy = strlen($exp[1]);
$dp = array(
0=>1,
1=>0.1,
2=>0.01,
3=>0.001,
4=>0.0001);
foreach ($dp as $key=>$acc)
{
if ($accuracy ==$key){
$step = $acc;
}
}
foreach (range($num1, $num2 ,$step) as $number) {
$ans.= "\"".$number."\",";
}
echo trim($ans,",");
}
If I give it two values eg 20.49 and 20.51 then the result is 20.49,20.5,20.51
How can I get the 20.5 to be 20.50?
Thanks
Rob
You don’t “get” 20.5 because php will automatically truncate trailing decimal 0’s. But, when you print the number with
echo, you can format it:In your loop, this could be done like this:
You can have something like this: