4

数値を含む変数の合計を取得しようとしています。一部は小数である可能性があります。これは小数点以下2桁にする必要があり、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ではなく17.5であることがわかります。おそらく文字列として指定されているためですか?

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"}

4

5 に答える 5

5

ラウンド形式、次に数値形式を使用できます。

  $total = 100+0+17.5+1+0.2;
//echo number_format((float)$total);  //119
  echo number_format(round((float)$total,2),2);  //118.50
于 2012-06-28T11:41:30.567 に答える
2

変数の値を確認してください$order->order->vat。varriableに割り当てた値を割り当てて追加すると、正解が表示されるため、これは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(回答)

またはfloatval($var) 変数のfloat値を取得するために使用します。

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

私はそれがうまくいくことを願っています。

ありがとう

于 2012-06-28T11:56:57.337 に答える
2

これらの変数はすべて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)

于 2012-06-28T11:58:31.450 に答える
1

修正や丸めを行わずに正確な値を探している場合は、bcaddの次の関数を使用して、実際の小数点以下2桁を取得する 必要があります。

$total =  $order->order->net+$order->order->deductions+$order->order->vat+$order->order->postage+$order->order->postage_tax; 
$total=bcadd(0,$total,2);
echo $total;
于 2017-04-01T22:25:34.673 に答える
0

通常、2dpに丸める関数は、「number_format」ではなく「round」です。

明らかに、あなたのエコーでは、それらは両方とも同じことをします。Roundを使用して将来の処理を行う場合は、Roundによって数値が永続的に調整されます。

そうは言っても、コードにエラーが表示されたり、0.5を削除する理由が表示されたりすることはありません。$ order-> order-> vatが実際には17ではなく17.5であることを確認することをお勧めします。17の場合は、それを逆方向に追跡して、整数化した場所を見つけます(たとえば、データベースまたは入力検証関数で、例)

于 2012-06-28T11:40:52.673 に答える