Click to See Complete Forum and Search --> : Limiting Decimals:


lavashark.com
10-03-2005, 10:01 PM
Visitors can rate authors on my site.
I display the average rating via:
<?php echo $admin_authoravgrating.':'.$authoravgrating ?>

How can I limit the return
from:
Average Rating::4.258095391
to:
Average Rating::4.25


This might help:
function getauthorbyid2($authorid)
{
$sql = "SELECT a.*,count(*) AS cnt,avg(n.rating) AS avgrating FROM authors a join news n on a.authorid=n.authorid WHERE a.authorid='$authorid' GROUP BY authorid";
$result = $this->select($sql);
return $result;
}

theuedimaster
10-03-2005, 11:02 PM
Read this about php rounding:

http://us2.php.net/manual/en/function.round.php

NogDog
10-04-2005, 01:11 AM
<?php echo sprintf("%s: %01.2f", $admin_authoravgrating, $authoravgrating); ?>

LimpBagel
10-04-2005, 09:39 AM
Or number_format() if you prefer.

$average = number_format($avgRating, 2);

chrys
10-04-2005, 09:52 AM
the easiest solution is:

round( $var, 2 );

2 being the number of numbers to have after the decimal.

If you every want to change a number like:

1123890 into 1,123,890, then use:

number_format( $var );

Simple :)

lavashark.com
10-04-2005, 04:44 PM
thanks again chrys