我的数据库中的十进制值为15,4。
我有一个数字-23.425,我想将其舍入到-23.42
我尝试了以下方法,但它们似乎都取整为-23.43:
sprintf("%.2f", $discountQueryResult['value'])
floor($discountQueryResult['value']*100)/100
还有其他方法可以降低小数点后第三位吗?
答案 0 :(得分:1)
您要舍入负数的数字。用floor()
将向下舍入为负数将增加其绝对值;请记住,-23.43(0)
比-23.425
少!
相反,您希望将{em>的值用ceil()
进行四舍五入:
echo ceil(-23.425 * 100) / 100; // -23.42
可以在here上看到它。
答案 1 :(得分:0)
尝试以下代码
return $rounded = 0.01 * (int)($number * 100);
还有其他类似的答案
1)PHP dropping decimals without rounding up