【问题标题】:PHP float numbers not adding correctly - decimal value missingPHP浮点数未正确添加 - 缺少十进制值
【发布时间】:2012-06-29 21:59:26
【问题描述】:

我正在尝试获取包含数字的变量的总数,其中一些可能是小数。我需要将其保留到小数点后两位,并且正在使用 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, '.', '');?>

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

100+0+17.5+1+0

我对此进行了研究并发现了以下内容

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

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

*编辑 下面是 $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"}

【问题讨论】:

  • 118.50 与 118.00 有很大不同,所以我不认为它是浮点错误。它只发生在非常大/小的数字上。你能给一个 $order 的 var_dump 吗?
  • 如果您以原始格式传递数字,it works。回显计算中使用的所有变量,并验证它们是否真的是你想/说的那样。

标签: php floating-point number-formatting


【解决方案1】:

一般情况下,您希望舍入到 2dp 的函数是“round”而不是“number_format”。

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

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

【讨论】:

    【解决方案2】:

    您可以使用圆形然后数字格式:

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

    【讨论】:

      【解决方案3】:

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

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

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

      或使用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, '.', '');?>
      

      我希望它会工作。

      谢谢

      【讨论】:

        【解决方案4】:

        您确定所有这些变量都是 int 或 float 类型吗? 检查类型为

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

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

        查看示例,

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

        结果:int(118)

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

        结果:float(118.5)

        【讨论】:

        • 数字是字符串类型。我不得不转换为浮点数,现在它可以工作了。谢谢
        【解决方案5】:

        如果您正在寻找没有任何校正或四舍五入的精确值,您应该使用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;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-28
          • 1970-01-01
          相关资源
          最近更新 更多