PHP 浮点数添加不正确 - 缺少十进制值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11243678/
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-08-25 00:04:39  来源:igfitidea点击:

PHP float numbers not adding correctly - decimal value missing

phpfloating-pointnumber-formatting

提问by LeeTee

I am trying to get the total of my variables containing numbers, some may be decimals. I need this to be to two decimal places and am using the number_format() function.

我正在尝试获取包含数字的变量的总数,有些可能是小数。我需要这是两位小数,并且正在使用 number_format() 函数。

$total =  $order->order->net+$order->order->deductions+$order->order->vat+$order->order->postage+$order->order->postage_tax; 
            echo number_format((float)$total, 2, '.', '');?>

I have noticed the following values do not add up correctly and seems to be ignoring the decimal. The total should be 118.50 but instead I get 118.00.

我注意到以下值加起来不正确,似乎忽略了小数。总数应该是 118.50,但我得到了 118.00。

100+0+17.5+1+0

100+0+17.5+1+0

I have researched this and found the below

我对此进行了研究,并找到了以下内容

http://floating-point-gui.de/basic/

http://floating-point-gui.de/basic/

I am a little confused by it. Can anyone explain what I need to do?

我有点困惑。谁能解释一下我需要做什么?

*EDITBelow is dump of the $order variable showing the numbers I am trying to add up. You can see the 17.5 is 17.5 and not 17. Is it because they are specified as being strings perhaps?

*编辑下面是 $order 变量的转储,显示了我试图加起来的数字。您可以看到 17.5 是 17.5 而不是 17。是否因为它们被指定为字符串?

object(SimpleXMLElement)#12 (21) { ["id"]=> string(6) "922704" ["shopkeeper_orderno"]=> string(4) "1001" ["customer"]=> string(6) "797893" ["creationdate"]=> string(16) "29-05-2012 11:55" ["net"]=> string(3) "100" ["vat"]=> string(4) "17.5" ["status"]=> string(1) "1" ["isnew"]=> string(1) "0" ["deductions"]=> string(1) "0" ["postage"]=> string(1) "1" ["paymentmethod"]=> string(20) "PayPal " ["instructions"]=> object(SimpleXMLElement)#17 (0) { } [2]=> object(SimpleXMLElement)#22 (1) { ["items"]=> object(SimpleXMLElement)#30 (9) { ["id"]=> string(7) "1384486" ["headerID"]=> string(6) "922704" ["productID"]=> string(7) "4959678" ["description"]=> string(13) "Wedding dress" ["net"]=> string(3) "100" ["vat"]=> string(4) "17.5" ["qty"]=> string(1) "1" ["formID"]=> string(2) "-1" ["options"]=> object(SimpleXMLElement)#31 (1) { ["options"]=> array(2) { [0]=> object(SimpleXMLElement)#32 (6) { ["id"]=> string(6) "519981" ["orderDetailsID"]=> string(7) "1384486" ["optionid"]=> string(6) "646934" ["optionCost"]=> string(1) "0" ["optionVAT"]=> string(1) "0" ["customText"]=> string(9) "size : 12" } [1]=> object(SimpleXMLElement)#33 (6) { ["id"]=> string(6) "519982" ["orderDetailsID"]=> string(7) "1384486" ["optionid"]=> string(6) "647285" ["optionCost"]=> string(1) "0" ["optionVAT"]=> string(1) "0" ["customText"]=> string(14) "Colour : Ivory" } } } } } } ["postage_tax"]=> string(1) "0" ["dispatched"]=> string(1) "0" ["paybyotherid"]=> string(2) "-1" ["wheredidyouhearid"]=> string(2) "-1"}

object(SimpleXMLElement)#12 (21) { ["id"]=> string(6) "922704" ["shopkeeper_orderno"]=> string(4) "1001" ["customer"]=> string(6) "797893" ["creationdate"]=> string(16) "29-05-2012 11:55" ["net"]=> string(3) "100" ["vat"]=> string(4) "17.5" ["status"]=> string(1) "1" ["isnew"]=> string(1) "0" ["deductions"]=> string(1) "0" ["postage"]=> string(1) "1" ["paymentmethod"]=> string(20) "PayPal " ["instructions"]=> object(SimpleXMLElement)#17 (0) { } [2]=> object(SimpleXMLElement)#22 (1) { ["items"]=> object(SimpleXMLElement)#30 (9) { ["id"]=> string(7) "1384486" ["headerID"]=> string(6) "922704" ["productID"]=> string(7) "4959678" ["description"]=> string(13) "Wedding dress" ["net"]=> string(3) "100" ["vat"]=> string(4) "17.5" ["qty"]=> string(1) "1" ["formID"]=> string(2) "-1" ["options"]=> object(SimpleXMLElement)#31 (1) { ["options"]=> array(2) { [0]=> object(SimpleXMLElement)#32 (6) { ["id"]=> string(6) "519981" ["orderDetailsID"]=> string(7) "1384486" ["optionid"]=> string(6) "646934" ["optionCost"]=> string(1) "0" ["optionVAT"]=> string(1) "0" ["customText"]=> string(9) "size : 12" } [1]=> object(SimpleXMLElement)#33 (6) { ["id"]=> string(6) "519982" ["orderDetailsID"]=> string(7) "1384486" ["optionid"]=> string(6) "647285" ["optionCost"]=> string(1) "0" ["optionVAT"]=> string(1) "0" ["customText"]=> string(14) "Colour : Ivory" } } } } } } ["postage_tax"]=> string(1) "0" ["dispatched"]=> string(1) "0" ["paybyotherid"]=> string(2) "-1" ["wheredidyouhearid"]=> string(2) "-1"}

采纳答案by Tufan Bar?? Y?ld?r?m

Are you sure your all of theese vars are intor floattype? Check the type as

你确定你所有的这些变量都是intfloat类型吗?检查类型为

var_dump($order->order->net, $order->order->deductions, $order->order->vat,$order->order->postage, $order->order->postage_tax);

if you used number_formatfor these vars they may be string, use floatval().

如果您使用number_format这些变量,它们可能是字符串,请使用floatval()。

check the examples,

检查示例,

$a = 100+0+"17,5"+1+0;
    var_dump($a);

Result : int(118)

结果:整数(118)

$b = 100+0+17.5+1+0;
    var_dump($b);

Result : float(118.5)

结果:浮动(118.5)

回答by MilMike

you can use round and then number format:

您可以使用舍入然后使用数字格式:

  $total = 100+0+17.5+1+0.2;
//echo number_format((float)$total);  //119
  echo number_format(round((float)$total,2),2);  //118.50

回答by Er. Anurag Jain

Please check the value coming for varriable $order->order->vat. I think this is not coming 17.5 because if We assign the value you assign to varriable and add them it shows correct answer.

请检查 varriable 的值$order->order->vat。我认为这不会是 17.5,因为如果我们分配您分配给变量的值并添加它们,它会显示正确的答案。

$a = 100;
$b = 0;
$c = 17.5;
$d = 1;
$e = 0;

$total = $a + $b + $c + $d + $e; echo number_format((float)$total, 2, '.', ''); //118.50 (Answer)

$total = $a + $b + $c + $d + $e;echo number_format((float)$total, 2, '.', ''); //118.50(答案)

Or Use floatval($var)for getting float value of variable.

floatval($var)用于获取变量的浮点值。

$total =  $order->order->net+$order->order->deductions+floatval($order->order->vat)+$order->order->postage+$order->order->postage_tax; 
 echo number_format((float)$total, 2, '.', '');?>

I hope it will be work.

我希望它会起作用。

thanks

谢谢

回答by jewelhuq

If you are looking for exact value without any correction or round you should use the bcaddfollowing function to get the actual two decimal point

如果您正在寻找没有任何更正或舍入的精确值,您应该使用bcadd以下函数来获得实际的两位小数点

$total =  $order->order->net+$order->order->deductions+$order->order->vat+$order->order->postage+$order->order->postage_tax; 
$total=bcadd(0,$total,2);
echo $total;

回答by Robbie

Generally, the function you want for rounding to 2dp is "round" and not "number_format".

通常,您想要四舍五入到 2dp 的函数是“round”而不是“number_format”。

Visibly, in your echo, they will both do the same, though. Round will permanently adjust the number if you want to do future processing with it.

显然,在你的回声中,他们都会做同样的事情。如果您想用它进行未来处理,Round 将永久调整该数字。

Having said that, I can't see any error in your code, or any reason why the 0.5 should be dropped. I suggest you check that $order->order->vat is actually 17.5 and not 17. If it is 17, then chase it backwards to find where you've integered that (e.g. in a database or in a input validation function, for example)

话虽如此,我在您的代码中看不到任何错误,也看不到应该删除 0.5 的任何原因。我建议您检查 $order->order->vat 实际上是 17.5 而不是 17。如果是 17,则向后追查它以找到您已对其进行整数化的位置(例如,在数据库或输入验证函数中,对于例子)