464,271 Members | 1,274 Online Need help? Post your question and get tips & solutions from a community of 464,271 IT Pros & Developers. It's quick & easy.

# how to calculate row total in dynamic form?

 P: n/a I'm sure this is very simple, but I have very little experience with javascript -- and what I do know isn't helping me here. I have a simple form where users can enter a quantity (qty) and cost (cost). Users can dynamically add rows to the table so I don't know how many rows might need to be calculated. I need to calculate the total (qty * cost) and put that number in a table cell (or read only input box). I also need to sum the totals for a grand total. can someone point me in the right direction?
[PUT TOTAL HERE]
[PUT GRAND TOTAL HERE]
Jun 18 '06 #1
4 Replies

 P: n/a Rich_C wrote: I'm sure this is very simple, but I have very little experience with javascript -- and what I do know isn't helping me here. I have a simple form where users can enter a quantity (qty) and cost (cost). Users can dynamically add rows to the table so I don't know how many rows might need to be calculated. I need to calculate the total (qty * cost) and put that number in a table cell (or read only input box). I also need to sum the totals for a grand total. can someone point me in the right direction?
[PUT TOTAL HERE]
[PUT GRAND TOTAL HERE]
[PUT TOTAL HERE]
[PUT GRAND TOTAL HERE]
Jun 18 '06 #2

 P: n/a TheBagbournes wrote on 18 jun 2006 in comp.lang.javascript: Then your form would be...
[PUT TOTAL HERE]
Qty: Cost: [PUT TOTAL HERE]
Qty: Cost: [PUT TOTAL HERE]
This works in a multirow table on each row separately: -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) Jun 18 '06 #3

 P: n/a Rich_C wrote: I'm sure this is very simple, but I have very little experience with javascript -- and what I do know isn't helping me here. I have a simple form where users can enter a quantity (qty) and cost (cost). Users can dynamically add rows to the table so I don't know how many rows might need to be calculated. I need to calculate the total (qty * cost) and put that number in a table cell (or read only input box). I also need to sum the totals for a grand total. Below is something that should help. Note that you must validate input before trying to do maths with it. Presuming you are using \$ and cents, work in whole cents internally, display as \$ only for output - same for any other currency. Read the FAQ on rounding and maths in JavaScript - unless you *want* to work in decimal cents, in which case you'd better decide how many decimal places you want to work to, then bone-up on rounding and the precision of maths in JavaScript (there are many posts in the archives). And remember that anyone with JavaScript disabled or not available won't see the results.
Qty Description Cost \$ Total \$
Grand Total 0
-- Rob Jun 19 '06 #4

 P: n/a JRS: In article <11*********************@c74g2000cwc.googlegroups. com>, dated Sun, 18 Jun 2006 18:12:38 remote, seen in news:comp.lang.javascript, RobG posted : Presuming you are using \$ andcents, work in whole cents internally, display as \$ only for output -same for any other currency. That applies in the OP's case. It occurs to me, however, that if VAT, sales taxes, etc., become involved, then merely working in cents may not be sufficient. VAT here adds 17.5% to the price, which is +7/40, so one could get exact results by working in hundredths of a cent; but the tax is 7/47 of the purchase price... In any such case, it is essential that the official rules for calculation be followed. There has been recent discussion, in news:borland.public.attachments and news:borland.public.delphi.language.delphi.general , of a Decimal Math Unit. Like Javascript, Delphi has no direct support for decimal arithmetic. In Javascript terms, IIRC AFAICS &c, the idea is to represent a "Decimal" as an Object containing an integer Mantissa part, an integer part representing a base-10 exponent, and parts indicating the desired accuracy (i.e. 2 decimal places (generally) for tangible currency) and type of rounding to apply. Operations would be performed by Methods. It gives an accuracy of about 15 significant figures over a range exceeding the wildest dreams of the Chancellor of the Exchequer. The idea is that by using such one could exactly match the requirements of law and accounting. Note that if these represent Currency there should be no need for a Multiply Together method. -- © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 © ? JL/RC: FAQ of news:comp.lang.javascript jscr maths, dates, sources. TP/BP/Delphi/jscr/&c, FAQ items, links. Jun 19 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion. 