简体   繁体   English

如何在获得评分时将平均值限制为两位小数?

[英]how do I limit the average into 2 decimal places in getting the rating?

average rating 平均评分

Here is my code! 这是我的代码! How do I get the average that has only 2 decimal places? 如何获得只有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'];
 }

how do I limit the average into 2 decimal places in getting the rating? 如何在获得评分时将平均值限制为两位小数?

http://php.net/round 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);
 }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM