doupu7651 2019-03-15 11:58
浏览 19
已采纳

php mysqli获取数据并将两个变量分成固定(2)/两位小数

I have been able to divide two variables from a mysqli query... How do i divide the number to two decimal places / toFIxed(2)

php

$Date = $_GET['date'];
    $Win = 'Win';

$testsql="
SELECT 
count(*) AS bet_count,
SUM(IF(result ='$Win', 1, 0)) AS win_count
FROM bets WHERE betDate = '$Date' GROUP BY betDate 
";

$testresult = mysqli_query($connection, $testsql);

while ($testrow = mysqli_fetch_assoc($testresult))
{ 
    echo "<tr>";
echo "<td class='text-center'>".($testrow['bet_count']/$testrow['win_count']). "</td>";
echo "</tr>";
}

So, the bet_count / win_count works as expected.....I just need the integer e.g. 2.371237234 to two decimal places 2.37

  • 写回答

1条回答 默认 最新

  • drjyvoi734793 2019-03-15 12:02
    关注

    You may try with number_format() function:

    <?php
    $Date = $_GET['date'];
    $Win = 'Win';
    
    $testsql = "
       SELECT 
          count(*) AS bet_count,
          SUM(IF(result ='$Win', 1, 0)) AS win_count
       FROM bets WHERE betDate = '$Date' GROUP BY betDate 
    ";
    
    $testresult = mysqli_query($connection, $testsql);
    
    while ($testrow = mysqli_fetch_assoc($testresult)) { 
        echo "<tr>";
        $value = number_format($testrow['bet_count']/$testrow['win_count'], 2, '.', '');
        echo "<td class='text-center'>".$value."</td>";
        echo "</tr>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog