簡體   English   中英

PHP浮點數未正確添加 - 缺少十進制值

[英]PHP float numbers not adding correctly - decimal value missing

我試圖得到包含數字的變量總數,有些可能是小數。 我需要這是兩位小數,並使用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" 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" }

你可以使用round和then數字格式:

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

請檢查可變$order->order->vat 我認為這不會是17.5,因為如果我們分配你賦予varriable的值並添加它們,它會顯示正確的答案。

$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(答案)

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

我希望它會起作用。

謝謝

你確定你所有的thears vars都是intfloat類型嗎? 檢查類型為

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);

結果:浮動(118.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;

通常,要舍入到2dp的函數是“round”而不是“number_format”。

可見,在你的回聲中,他們都會做同樣的事情。 如果您希望將來使用它,Round將永久調整該數字。

話雖如此,我看不到代碼中的任何錯誤,或者為什么要刪除0.5的任何原因。 我建議你檢查$ order-> order-> vat實際上是17.5而不是17.如果它是17,那么追逐它以找到你所交換的位置(例如在數據庫或輸入驗證函數中,例)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM