Fix Sum result php mysql

Asked

Viewed 88 times

-2

Code used to obtain 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

sum result in php and mysql

R$: 237688.6

Desired result

R$: 237688.60

Personal I used the code published by Eduardo Silva that solves my problem in several parts in my system but I found a code that may help many people here

Number formatting (php)

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>"; } ?>

  • Please show us what you did to get your results

  • Not as a comment Cristiano, edit your question and put the code there, it is easier to view

  • 1

    Wouldn’t it be better if you changed the size of the decimal place in your database column? It would be less code for PHP to process

  • is thus decimal (10,2)

1 answer

2


Use the function number_format():

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.
  • Thank you Eduardo

  • Eduardo has how to help me format my code from this part there? I’m not able to display this way that Oce put only using several echo

  • Your full code is the one you added to the question, @Cristianocardososilva?

  • That same Eduardo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.