Calculation problem ?

1. Member
Join Date
Dec 1969
Posts
47

## Calculation problem ?

Hi all,<BR><BR>been busy buiding a page with some INPUT-fields that are used in a calculation.<BR>Most of the values are calculated correctly but 2 of them keep buggin&#039; me.<BR>It&#039;s iteVal and vefVal...... The numbers get put AFTER each other and not added up like they are supposed to.... - sigh - .<BR><BR>I guess I&#039;m a bit of a newbie so I hope 1 of y&#039;all out there finds the time to help me out or point me in a direction where I can learn... <BR>Greetz Orb_X<BR><BR>//CODE<BR>&#060;SCRIPT Language="JavaScript"&#062; <BR>function pretty( num ) <BR>{ <BR>var str = "" + Math.round( num * 100.0); <BR>var slen = str.length; <BR>switch( slen ) <BR>{ <BR>case 0: // can&#039;t happen??? <BR>return "0.00"; <BR>case 1: <BR>return "0.0" + str; <BR>case 2: <BR>return "0." + str; <BR>default: <BR>return str.substring(0,slen-2) + "," + str.substring(slen-2); <BR>} <BR>} <BR>function fixnum( val ) <BR>{ <BR>return val.replace(",","."); <BR>} <BR>function reCalc( ) <BR>{ <BR>var frm = document.form1; <BR><BR>frm.ink_in_f.value = frm.ver_ex_f.value = frm.ver_in_f.value = "---"; <BR><BR>iefVal = fixnum( frm.ink_ex_f.value ); <BR>if ( isNaN( iefVal ) ) return;<BR>aeVal = fixnum(frm.aanl_ex.value);<BR>if ( isNaN(aeVal)) return;<BR>mVal = fixnum(frm.marge_f.value);<BR>if ( isNaN( mVal ) ) return; <BR><BR>iifVal = 1.19 * iefVal ; <BR>frm.ink_in_f.value = pretty( iifVal ); <BR><BR>aiVal= 1.19 * aeVal;<BR>frm.aanl_in.value= pretty(aiVal);<BR><BR>iteVal = iefVal + aeVal ;<BR>frm.inktotex.value=pretty(iteVal);<BR><BR>iti Val = iifVal + aiVal ;<BR>frm.inktotin.value=pretty(itiVal);<BR><BR>vef Val = iteVal * (( mVal / 100.0 ) + 1.0 ); <BR>frm.ver_ex_f.value = pretty(vefVal); <BR><BR>vifVal = (itiVal * (( mVal /100.0) + 1.0 )) * 1.19; <BR>frm.ver_in_f.value = pretty(vifVal); <BR><BR>frm.info.focus(); <BR>}<BR>&#060;/SCRIPT&#062;

2. Member
Join Date
Dec 1969
Posts
47

## Posted code with regards to B. Wilkinson <eom

.

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## + can mean concatenate in JS...

JS uses + to mean *both* addition and concatenation.<BR><BR>And the ".value" property of a form field is *ALWAYS* a string! (Even if it looks visually like a number.) So adding to a form field value often *does* do concatenation, instead. To fix it, just be *sure* to convert the form field&#039;s .value to a number using parseInt or parseFloat.<BR><BR>So I *think* this will fix all the bugs:<BR><BR>function fixnum( val ) <BR>{ <BR>&nbsp; &nbsp; return parseFloat( val.replace(",",".") ); <BR>}<BR><BR>The reason I didn&#039;t notice that before is because I was multiplying values, and the * operator has only one meaning, not two like the + operator.<BR><BR>Sorry about that.<BR><BR>

4. Member
Join Date
Dec 1969
Posts
47

## Thanx Mr. B. Wilkinson :-)

Thank you for helping me out - again - !!!<BR><BR>That double meaning thing ... I didn&#039;t know that.<BR>Guess I&#039;m really a newbie :-)<BR><BR>Greetz Orb_X<BR><BR>PS Checked your site, nice to meet you, GURU (all hail ! :-) <BR> My home-site : http://fly.to/Terrok-Nor ;-)<BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Error when view your page...

In that "basic.js" file you include, on line 48:<BR><BR>r=top.document.referrer<BR><BR>"Access denied"<BR><BR>No idea why.<BR><BR><BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•