I am trying to calculate prices for a loop of elements :
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 ;
}
My PHP loop is :
<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>
My problem is if I add tcost .toFixed(0)
it calculates the round values of the total cost in each row. Suppose the values are 36,50.8 and 23.64. It calculates as 36 + 51 + 24 = 111. But I want the round value of total of each row, ie 36 + 50.8 + 23.64 = 110.44 which can rounds up as 110.00 . Can you suggest me how I can achieve this ?
UPDATE: its like suppose you have 3 loops. In the first loop , total row value is 36, second loop the total row value is 50.8( so total value for now is 36 + 50.8 ) . third loop row value is 23.64 ( so total value is 36+50.8+23.64) . If I add toFixed(0) to the total, the addition will be like ( 36 + 51 + 24 ) = 111 . But client need the addition to be like 36 + 50.8 + 23.64 = 110.44 , now round value of this is 110.00 . Hope question is clear now