0

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

Nitish
  • 2,695
  • 9
  • 53
  • 88
  • its in php file..`for loop` – Nitish May 29 '13 at 09:11
  • Are rows in a php file too? I'm not sure how your'e mixing server side and client side, but you can add a variable into your function, add `tcost` to this on every iteration. After the loop, round the value of the variable and show it on the page. If this won't work, we'll need to see more code... – Teemu May 29 '13 at 09:18

3 Answers3

0

use parseFloat

that is for a = 30 b = 50.8 c = 23.64

value = parseFloat(a) + parseFloat(b) + parseFloat(c);
intivev
  • 43
  • 1
  • 5
  • "`... I want the round value of total ...`", `parseFloat()` is not for rounding... :-(. – Teemu May 29 '13 at 09:20
0

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!?

Also your calculation is incorrect

30 + 50.8 + 23.64 = 104.44

And in javascript:

var total = Number(30) + Number(50.8) + Number(23.64);
alert(total.toFixed()) // gives you '104'

Hope that helps some what

lemonSkip
  • 54
  • 5
  • I need to add the variable `total` for the next row also. So I nedd the individual rows without round but the final of all the row total in round value – Nitish May 29 '13 at 09:25
  • Its for javascript. My client needs to calculate immediately the total values. So basically its a javascript question where loop of values are added. – Nitish May 29 '13 at 09:29
0

It seems that you want to round the value. There are 3 functions in Math:

  • Math.floorMDN
  • Math.ceilMDN: Returns the smallest integer greater than or equal to a number.
  • Math.roundMDN: Returns the value of a number rounded to the nearest integer.

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

I think you need Math.round :

Math.round ( 110.44 ) //110
Bakudan
  • 19,134
  • 9
  • 53
  • 73