Pablo Servin Pablo Servin - 2 years ago 68
PHP Question

round 2 decimals in a multiplication php is not rounding

I come to you looking for answers, I have a table and I am calculating the tax field multiplying 1 variable per 0.16 but I want it to have just 2 decimals, not 10 or 4 or 3, I was trying with round() and with number_format() but is not working, there must be something I am doing wrong, how it suppose that I need to do it ?

I am showing the code

$html .= '<tr>
<td><img src="functions/'.escape($r->foto).'"></td>
<td>$'.escape($r->number_format(preciounitario*0.16,2)).'</td> //this is the one with the error
$totalV += $listNum[$i]*$r->totalcosto;
$i = $i+1;

Answer Source

If you are just looking to round a number, then here is the solution:

    $number = 1353.234242;
    $rounded = (float)sprintf('%.2f', $number);
    echo $rounded; // will display 1353.23
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download