Cutting decimals

Hi everyone,

After trying a few things - and failing - I found this forum and I would like to ask your help. So I have this code, it works fine, but the number I get as a result is like 4653.67586846453664
I can’t figure out how to make the resulted number 4652.67 instead.

What the code does is getting member’s earnings from the database, adding them and putting out the total.

I tried to use number_format() but I just couldn’t make it right.

Here is the code, please tell me how to change it, what to add and where.

[php]<?php
$dbhost = ‘localhost’;
$dbuser = ‘user’;
$dbpass = ‘password’;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ’ . mysql_error());
}
$sql = ‘SELECT userid, SUM(total_paid) FROM members GROUP BY approved’;

mysql_select_db(‘databasename’);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ’ . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "$ {$row[‘SUM(total_paid)’]}
";
}
mysql_close($conn);
?>[/php]

Thank you in advance.

use

number_format($var, 2).

the 2 is number of decimal places past the.

Sponsor our Newsletter | Privacy Policy | Terms of Service