这里的标题可能很普通,但是我在php.net上的代码中搜索了最近5个小时,但这里没有匹配的问题。我的问题是我想计算两个变量。但是为什么它的计算不正确。
$qty = 0;
$cqty = 0;
$price = 0;
$cprice = 0;
$gprice = 0;
$hprice = 0;
$tprice = 0;
$i = 1;
foreach( $details as $index => $detail) {?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $detail['item_name']; ?></td>
<td class="right"><?php echo $detail['quantity']; ?></td>
<td class="right"><?php echo number_format((float)$detail['purchase_price'], 2); ?></td>
<td class="right"><?php echo number_format((float)$detail['quantity']*$detail['purchase_price'], 2); ?></td>
<td><?php echo $details2[$index]['item_name']; ?></td>
<td class="right"><?php echo $details2[$index]['cquantity']; ?></td>
<td class="right"><?php echo number_format((float)$details2[$index]['cpurchase_price'], 2); ?></td>
<td class="right"><?php echo number_format((float)$details2[$index]['cquantity']*$details2[$index]['cpurchase_price'], 2); ?></td></tr>
<?php
$qty += $detail['quantity'];
$cqty += $details2[$index]['cquantity'];
$price += (number_format((float)$detail['quantity'] *$detail['purchase_price'], 2));
$cprice += (number_format((float)$details2[$index]['cquantity'] * $details2[$index]['cpurchase_price'], 2));
$gprice = (double) $price;
$hprice = (double) $cprice;
$tprice += $gprice + $hprice;
$i++;
} ?>
</tbody>
</table>
</div>
<div class="space20"></div>
<div class="row-fluid">
<div class="span4 invoice-block pull-right">
<ul class="unstyled amounts">
<li><strong>Sub - Total amount :</strong> <?php echo number_format((float)$price, 2); ?> </li>
<li><strong>Grand Total :</strong> <?php echo number_format((float)$cprice, 2); ?> </li>
<li><strong>cccGrand Total :</strong> <?php echo number_format((float)$tprice, 2); ?> </li>
</ul>
</div></div>
我的问题是显示price + cprice = tprice之间的正确计算。假设价格= 170,价格= 520,所以价格= 690。但是输出显示tprice = 1890。我完全无法弄清楚输出是如何计算的。 here is screenshot
答案 0 :(得分:0)
尝试一下,只是在计算值时不要使用number_format,因为它会给出字符串而不是数字:
$qty += $detail['quantity'];
$cqty += $details2[$index]['cquantity'];
$price += (float)$detail['quantity'] *$detail['purchase_price'];
$cprice += (float)$details2[$index]['cquantity'] * $details2[$index]['cpurchase_price'];
$gprice = (double) $price;
$hprice = (double) $cprice;
$tprice += $gprice + $hprice;
$i++;