不可能舍入或截断十进制 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42899702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:36:50  来源:igfitidea点击:

Impossible to round or truncate decimal Laravel

phplaravel

提问by K4tn1x

I have a problem in my Controller in Laravel 4. I try to round (or truncate) a decimal with 2 digits after comma, but it doesn't work.

我在 Laravel 4 的控制器中遇到问题。我尝试舍入(或截断)逗号后有 2 位数字的小数,但它不起作用。

I tried something like this :

我试过这样的事情:

round(888/100, 2);

Or :

或者 :

floor(888*100)/100;

But I have 8.880000000000001 instead of 8.88. Why?

但我有 8.880000000000001 而不是 8.88。为什么?

(In this example I take this number but it can be an other number)

(在这个例子中,我使用这个数字,但它可以是其他数字)

回答by Bara' ayyash

Try this after rounding :

舍入后试试这个:

number_format((float)$number, 2, '.', '');

回答by M.Elwan

try this

尝试这个

number_format($number, 2, '.', ',');
// 2 = decimal places |  '.' = decimal seperator | ',' = thousand seperator