PHP舍入小数而不四舍五入 [英] PHP dropping decimals without rounding up
本文介绍了PHP舍入小数而不四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想舍入小数而不四舍五入.例如,如果我有1.505,我想删除最后一个小数,值应为1.50. PHP中有这样的功能吗?
I want to drop off decimals without rounding up. For example if I have 1.505, I want to drop last decimal and value should be 1.50. Is there such a function in PHP?
推荐答案
您需要通过以下方式 floor():
$rounded = floor($float*100)/100;
或者您将其强制转换为整数:
Or you cast to integer:
$rounded = 0.01 * (int)($float*100);
这样,它将不会四舍五入.
This way it will not be rounding up.
这篇关于PHP舍入小数而不四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文