[英]Javascript calculation for a data loop with no decimals
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 : 我的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>
My problem is if I add tcost .toFixed(0)
it calculates the round values of the total cost in each row. 我的问题是,如果我添加
tcost .toFixed(0)
它将计算每一行总成本的取整值。 Suppose the values are 36,50.8 and 23.64. 假设值为36,50.8和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 .
它的计算结果为36 + 51 + 24 =111。但是我想要每行总计的取整值,即36 + 50.8 + 23.64 = 110.44,可以取整为110.00。 Can you suggest me how I can achieve this ?
您能建议我如何实现这一目标吗?
UPDATE: its like suppose you have 3 loops. 更新:假设您有3个循环。 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 ) .
在第一个循环中,总行值为36,第二个循环中总行值为50.8(因此,现在的总值为36 + 50.8)。 third loop row value is 23.64 ( so total value is 36+50.8+23.64) .
第三循环行的值为23.64(因此总值为36 + 50.8 + 23.64)。 If I add toFixed(0) to the total, the addition will be like ( 36 + 51 + 24 ) = 111 .
如果我将toFixed(0)加到总数上,则加法运算将类似于(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 .
但是客户需要加成为36 + 50.8 + 23.64 = 110.44,现在这个整数值为110.00。 Hope question is clear now
希望问题现在很清楚
use parseFloat 使用parseFloat
that is for a = 30 b = 50.8 c = 23.64 即a = 30 b = 50.8 c = 23.64
value = parseFloat(a) + parseFloat(b) + parseFloat(c);
Hi Reading your question is awfully confusing ... especially when you commented on it saying "its in the php file" ... I thought this is a javascript issue!? 嗨,阅读您的问题是非常令人困惑的...尤其是当您在评论中说“它在php文件中”时……我认为这是一个javascript问题!
Also your calculation is incorrect 另外你的计算不正确
30 + 50.8 + 23.64 = 104.44
And in javascript: 并且在javascript中:
var total = Number(30) + Number(50.8) + Number(23.64);
alert(total.toFixed()) // gives you '104'
Hope that helps some what 希望对你有所帮助
It seems that you want to round the value. 您似乎想舍入该值。 There are 3 functions in Math:
数学中有3个函数:
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
但是我想要每行总计的取整值,即36 + 50.8 + 23.64 = 110.44,可以将其取整为110.00
I think you need Math.round
: 我认为您需要
Math.round
:
Math.round ( 110.44 ) //110
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.