HighDots Forums  

Another rookie math question...

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss Another rookie math question... in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Edward
 
Posts: n/a

Default Another rookie math question... - 09-04-2005 , 10:24 AM






I'm trying to understand and display simple math results, but I get an
unexpected value. Hope this isn't too confusing.
I'd like to add quantities like 2 + 1 + 3 + 0 + 0 to = 6. But instead I
get a result like: 21300

-- I'm trying to get a total number of items ordered using:

var total1=eval(parseInt(f.item1_quan.value*23)) <- how cost total is
var total2=eval(parseInt(f.item2_quan.value*41)) derived for each item,
var total3=eval(parseInt(f.item3_quan.value*15)) for reference. Numbers
are
var total6=eval(parseInt(f.item6_quan.value*15)) item prices.
var total7=eval(parseInt(f.item7_quan.value*17))

- I added:
var disc_quan = (f.item1_quan.value + f.item2_quan.value +
f.item3_quan.value + f.item6_quan.value + f.item7_quan.value)

-- I'm displaying the result on my form for this test using:
<td colspan="2" align="right"><font face="Verdana" size="2"> Test Quantity
Total</td>
<td><input type="text" size="7" name="discount_qty" onFocus="this.blur()"
value="0" /></td></tr>

-- Legend for this post:
item1_quan: form field name for quantity of item
disc_quan: total quantity of all items, 2+3+1+0+0=6

I hope I have included enough info to make this understandable.

Any suggestions appreciated.





Reply With Quote
  #2  
Old   
Edward
 
Posts: n/a

Default Re: Another rookie math question... - 09-04-2005 , 11:48 AM






I fixed it!

I had to use "parseInt" to contion each field/variable from the form like:

First, I converted each form field to a variable: f.item1_quan.value -->
item1q.

var disc_quan = (parseInt(item1q) + parseInt(item2q) + parseInt(item3q) +
parseInt(item6q) + parseInt(item7q))

Apparently I had string values instead of pure numeric.


"Edward" <nomail (AT) yahoo (DOT) com> wrote

Quote:
I'm trying to understand and display simple math results, but I get an
unexpected value. Hope this isn't too confusing.
I'd like to add quantities like 2 + 1 + 3 + 0 + 0 to = 6. But instead I
get a result like: 21300

-- I'm trying to get a total number of items ordered using:

var total1=eval(parseInt(f.item1_quan.value*23)) <- how cost total is
var total2=eval(parseInt(f.item2_quan.value*41)) derived for each
item,
var total3=eval(parseInt(f.item3_quan.value*15)) for reference.
Numbers
are
var total6=eval(parseInt(f.item6_quan.value*15)) item prices.
var total7=eval(parseInt(f.item7_quan.value*17))

- I added:
var disc_quan = (f.item1_quan.value + f.item2_quan.value +
f.item3_quan.value + f.item6_quan.value + f.item7_quan.value)

-- I'm displaying the result on my form for this test using:
td colspan="2" align="right"><font face="Verdana" size="2"> Test Quantity
Total</td
td><input type="text" size="7" name="discount_qty" onFocus="this.blur()"
value="0" /></td></tr

-- Legend for this post:
item1_quan: form field name for quantity of item
disc_quan: total quantity of all items, 2+3+1+0+0=6

I hope I have included enough info to make this understandable.

Any suggestions appreciated.







Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.