我正在尝试计算元素循环的价格:
function calculateBill(id,price)
{
var t = document.getElementById('total').value;
var qty = document.getElementById('qty_'+id).value;
var total = qty * price;
var tcost = Number(t) + Number(total);
document.getElementById('total').value = tcost ;
}
我的 PHP 循环是:
<form name="sendgoods.php" method="post">
<?php
unset($data2);
unset($result2);
for( $i2 = 0; $i2<count($r2); $i2++ )
{
?>
<tr>
<td><?php echo $r2[$i2]['veg_name']; ?></td>
<td><?php echo $r2[$i2]['ret_price']; ?></td>
<td><?php echo $r2[$i2]['mop']; ?></td>
<td><?php echo $r2[$i2]['ret_margin']; ?></td>
<td>
<input type="text" id = "qty_<?php echo $i2; ?>" name="qty_<?php echo $i2; ?>" onkeyup="calculateBill('<?php echo $i2; ?>','<?php echo round($r2[$i2]['ret_price'],2); ?>')" />
<input type="hidden" name="veg_name_<?php echo $i2; ?>" value="<?php echo $r2[$i2]['veg_name']; ?>" />
<input type="hidden" name="rate_<?php echo $i2; ?>" value="<?php echo $r2[$i2]['avg_rate']; ?>" />
<input type="hidden" name="mop_<?php echo $i2; ?>" value="<?php echo $r2[$i2]['mop']; ?>" />
<input type="hidden" name="ws_price_<?php echo $i2; ?>" value="<?php echo round($r2[$i2]['ws_price'],2); ?>" />
<input type="hidden" name="ws_margin_<?php echo $i2; ?>" value="<?php echo $r2[$i2]['ws_margin']; ?>" />
<input type="hidden" name="ret_price_<?php echo $i2; ?>" value="<?php echo round($r2[$i2]['ret_price'],2); ?>" />
<input type="hidden" name="ret_margin_<?php echo $i2; ?>" value="<?php echo $r2[$i2]['ret_margin']; ?>" />
</td>
</tr>
<?php } ?>
</table>
<input type="hidden" name="customer_id" value="<?php echo $cid; ?>" />
<input type="hidden" name="dt" value="<?php echo $dt_str; ?>" />
<input type="hidden" name="customer_type" value="<?php echo $ctype_id; ?>" />
<input type="hidden" name="counter" value="<?php echo count($r2); ?>" />
<input type="hidden" name="add_goods" value="1" />
<div class="we">
<span class="dscnt">DISCOUNT - </span>
<input type="text" id="discount" name="discount" onchange="calculateDiscount()" />
<span class="billng_amnt">Total Billing amount - </span>
<input type="text" id="total" readonly="readonly" />
</div>
<input type="submit" value="SUBMIT" />
</form>
我的问题是如果我添加tcost .toFixed(0)
它会计算每行中总成本的整数值。假设值为 36,50.8 和 23.64。它计算为 36 + 51 + 24 = 111。但我想要每行总计的舍入值,即 36 + 50.8 + 23.64 = 110.44 可以四舍五入为 110.00 。你能建议我如何做到这一点吗?
更新:就像假设你有 3 个循环。在第一个循环中,总行值为 36,第二个循环总行值为 50.8(所以现在的总值为 36 + 50.8)。第三个循环行值为 23.64(因此总值为 36+50.8+23.64)。如果我将 toFixed(0) 添加到总数中,则加法将类似于 ( 36 + 51 + 24 ) = 111 。但是客户端需要加法为 36 + 50.8 + 23.64 = 110.44 ,现在这个值是 110.00 。希望问题现在很清楚