可能的重复:
如何将数字格式化为只有两位小数?
我已经四处寻找解决方案。假设我有一个带有这样数字的变量:
$price1 = 1597;
$price2 = 1497.85;
Run Code Online (Sandbox Code Playgroud)
有没有偶数时显示2位小数而不四舍五入的函数:
$price1 = 1597.00;
$price2 = 1497.85;
Run Code Online (Sandbox Code Playgroud)
更新:
我发现了问题,SUM(total) MySQL 函数将值四舍五入:
$result = mysql_query("SELECT SUM(total), SUM(subtotal) FROM jos_vm_order_list WHERE user_id = '$user_id'");
while($totalrow = mysql_fetch_array($result))
{
$total = $row['SUM(total)'];
$total = number_format($total, 2, '.', '');
$subtotal = $row['SUM(subtotal)'];
$subtotal = number_format($subtotal, 2, '.', '');
}
Run Code Online (Sandbox Code Playgroud)
无论如何它不能在使用 MySQL SUM() 函数时四舍五入吗?
提前感谢您的帮助。
小智 -4
使用 number_format PHP 函数:
number_format(your_number,otional_decimals,optional_decimal_point,optional_1000_separator)
Run Code Online (Sandbox Code Playgroud)
您不能仅指定小数点或 1000 分隔符,如果指定其中之一,则需要同时指定两者。当使用小数时,它的功能类似于 round(),并将数值向上 0.5 或以上,向下 0.5 以下。
就像下面这样:
number_format ($price, 2);
Run Code Online (Sandbox Code Playgroud)