php int 乘以 decimal 产生一个 int?

php multiplying int by decimal produces an int?

所以我试图理解这种行为: 在我的数据库 table 中,我有一个行数量 (int 11) 和一个列价格 (decimal 5,2)。 将它们相乘得到一个总数,输出一个整数……这怎么可能?

$qty = 2;
$price = 10;
$total = $qty * $price;

echo "$total";

// 输出20,不应该输出20,00吗?

因此,对于任何想知道的人,这里有一个 php 货币格式设置功能。 http://php.net/manual/it/function.money-format.php 查看手册以获取更多信息,感谢那些回答的人。

你还没有证明什么。 20 是正确的

$qty = 2;
$price = 10.23;
$total = $qty * $price;
echo "$total";

再次给出 20.46 正确的

如果你想格式化输出,使用

printf("%.2f", $total);

获得类似

的结果
20.00
20.46

要根据语言环境进行调整,money_format() 会更好。这会让你得到 , (或其他)而不是 .