舍去小数位而不舍入值

时间:2019-09-10 00:12:31

标签: php rounding

我的数据库中的十进制值为15,4。

我有一个数字-23.425,我想将其舍入到-23.42

我尝试了以下方法,但它们似乎都取整为-23.43:

 sprintf("%.2f", $discountQueryResult['value'])

 floor($discountQueryResult['value']*100)/100 

还有其他方法可以降低小数点后第三位吗?

2 个答案:

答案 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

2)PHP: get number of decimal digits

3)Remove a decimal place in PHP