php mysqli 获取数据并将两个变量除以Fixed(2)/两位小数

标签 php

我已经能够从 mysqli 查询中划分两个变量...我如何将数字划分为两位小数/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>";
}

因此,bet_count/win_count 按预期工作......我只需要整数,例如2.371237234 精确到小数点后两位 2.37

最佳答案

您可以尝试使用 number_format()功能:

<?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>";
}

关于php mysqli 获取数据并将两个变量除以Fixed(2)/两位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55182139/

相关文章:

php - Ubuntu 12.10 上的 PHP MySQL 问题

PHP:通过字符串值获取静态类的实例

php - 剥离ID的URL查询MYSQL数据库

php - #1064 - 将 .ibd 或 .frm 文件导入数据库时​​出错

php - 将 XML 导入 MySQL 数据库

php - 使用PHP+MySql的投票系统?

php - 无法在 Highcharts 中的多列 mysql 数组上显示 x 轴标签

php - CodeIgniter 重命名电子邮件附件文件

php - 从表中获取多行并将其写入excel

javascript - Uncaught ReferenceError : reg is not defined