Format in R $ values rescued with SUM

1

I want to display values in currency format

include "conect.php";
$sql = "SELECT SUM(preco) as SOMA, cod FROM valores GROUP BY cod ORDER BY SOMA DESC";
$exec = mysql_query($sql);

while ($rows = mysql_fetch_assoc($exec)) {
     echo $rows["cod"]."-".$rows["SOMA"]."<br><br><br>";

}

Preview:

BT01-151000

BT02-48000

BT03-22000

BT04-11000

Desired:

BT01- R $ 1.510,00

BT02- $ 480.00

BT03- $ 220.00

BT04- $ 110.00

    
asked by anonymous 11.08.2016 / 18:13

1 answer

3

You can do this using the php function number_format :

  

Ref: link

function formatar_valor($valor){
      return number_format($valor, 2, '.', '');
}


include "conect.php";
$sql = "SELECT SUM(preco) as SOMA, cod FROM valores GROUP BY cod ORDER BY SOMA DESC";
$exec = mysql_query($sql);

while ($rows = mysql_fetch_assoc($exec)) {
     echo $rows["cod"]."- R$ ".formatar_valor($rows["SOMA"])."<br><br><br>";
}
    
11.08.2016 / 18:19