Correct Sum result php mysql

-2

Code used to get the result

<?php
   $total = 0;
   while($row = mysql_fetch_object($trabalho)) {                                    
      $total += $row->valor;
      echo "<tr><td>$row->os</td><td>$row->descricao</td><td>$row->valor</td></tr>";
   }
   echo "<tr><td colspan="3">TOTAL: $total</td></tr>";
?>

How do I display the correct value

result of sum in php and mysql

R$: 237688.6

Desired result

R$: 237688.60

I used the code published by Eduardo Silva that solves my problem in several parts in my system but I found a code that maybe can help a lot of people here

Number formatting (php)

formatting code

<?php while($row = mysql_fetch_object($recibo_pagador)) { echo "<tr><td>$row->nome_empresa</td><td>$row->nome_cliente</td><td>$row->data_recibo</td><td>R$ "; echo number_format($row->valor_recibo,2,",","."); echo "</td><td><a href='print_recibo_recebedor.php?id=$row->id_recibo' class='btn grey darken-3'>Imprimir</a></td></tr>"; } ?>

    
asked by anonymous 04.05.2016 / 19:52

1 answer

2

Use the number_format () function:

echo '<tr><td colspan="3">TOTAL: ' . number_format($total, 2, ".", "") . '</td></tr>';

Where:

  • 1st parameter: number to be formatted.
  • 2nd parameter: number of decimal places.
  • 3rd parameter: decimal separator digit.
  • 4th parameter: thousands separator digit.
04.05.2016 / 20:09