如何限制表中值的小数位?

时间:2016-04-11 03:13:36

标签: php mysql arrays

在我的排行榜上:https://mgo.io/mgo3/leaderboard.php,我正在通过此代码计算一个战队的胜利%:

<td style="text-align: center;">
  {% set total = leader['win'] + leader['loss'] %}
  {% if total == 0 %}0{% else %}{{ (leader['win'] / total * 100)|e }}{% endif %}%
</td>

方括号内的文字信息EX:{{leader [&#39; xxx&#39;] | e}}正在通过.php文件传递:

function getLeaders() {
    global $dbh;

    date_default_timezone_set('UTC');

    $res = array();

    $stmt = $dbh->prepare("SELECT id, name, wins, losses, cp FROM clans ORDER BY id ASC");
    $stmt->execute();
    while ($row = $stmt->fetch()) {
        $clan_id = (int) $row['id'];
        $clan_name = $row['name'];

        $res[$clan_id] = array();
        $res[$clan_id]['name'] = $clan_name;
        $res[$clan_id]['rank'] = 0;
        $res[$clan_id]['cp'] = $row['cp'];
        $res[$clan_id]['win'] = $row['wins']; 
        $res[$clan_id]['loss'] = $row['losses'];

如何缩短从公式输出到第十位的百分比?例如:79.45653355%至79.4%

0 个答案:

没有答案