如何在获得评级时将平均值限制在2位小数?

时间:2016-03-04 16:39:05

标签: php codeigniter

average rating

这是我的代码!如何获得只有2位小数的平均值?

 public function getAveRating($restoid){ 
      $where=array(
           "restoid"=>$restoid
           );
      $this->db->where($where);
      $this->db->select_avg('rate');
      $query = $this->db->get('ratings')->first_row('array');
      return $query['rate'];
 }

如何在获得评级时将平均值限制在2位小数?

2 个答案:

答案 0 :(得分:0)

http://php.net/round

 public function getAveRating($restoid){ 
      $where=array(
           "restoid"=>$restoid
           );
      $this->db->where($where);
      $this->db->select_avg('rate');
      $query = $this->db->get('ratings')->first_row('array');
      return round($query['rate'], 2);
 }

答案 1 :(得分:0)

return $query['rate'];更改为return round($query['rate'],2);