basic problem

1. Senior Member
Join Date
Dec 1969
Posts
294

## basic problem

i want to add 2 decimal numbers and display it<BR>my code is :-<BR><BR>&#060;html&#062;<BR><BR>&#060;script language="javascript"&#062;<BR>function cal(f)<BR>{<BR> a=f.p1.value<BR> b=f.p2.value<BR> c=a+b<BR> f.result.value=c<BR>}<BR>&#060;/script&#062; <BR><BR>&#060;body&#062;&#060;form&#062;<BR>&#060; input type="text" name=p1 size=6&#062;<BR>&#060;input type="text" name=p2 size=6&#062;<BR>&#060;p&#062;<BR>&#060;input type="text" name="result" onclick="cal(this.form)"&#062;<BR>&#060;/form&#062;<BR>&#060;/script&#062;<BR>&#060;/body&#062;<BR>&#060;/html&#062;<BR><BR><BR>it is not adding. if i put 5 & 7 it shows 57 but i want 12<BR><BR>pl help me<BR>

2. Senior Member
Join Date
Dec 1969
Posts
19,082

## yes, very basic

function cal(f)<BR>{<BR>a=parseFloat(f.p1.value)<BR>b=parse Float(f.p2.value)<BR>c=a+b<BR>f.result.value=c<BR> }

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

...about how the DOM works.<BR><BR>*ALL* .value properties are STRINGS.<BR><BR>Always.<BR><BR>So you are doing <BR> c = a + b;<BR>and getting<BR> c = "5" + "7"<BR>and of course you get the *string* 57 as a result.<BR><BR>*NOW* try doing<BR> c = parseFloat(a) + parseFloat(b);<BR><BR>If those numbers are supposed to be integers (whole numbers) then you can use parseInt() instead of parseFloat().<BR><BR>You would probably find it useful to go to the http://www.irt.org site and look at their JavaScript FAQ.<BR><BR>

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

## See...if I were as succinct as you...

...I wouldn&#039;t be 12 seconds slower!<BR><BR>What are you doing here on a...Sunday? Must be. Early Sunday, too, no?<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
19,082

## RE: See...if I were as succinct as you...

yup, about 11am sunday - the joys of wireless internet (posting from bed).<BR><BR>ordinarily I don&#039;t even get up before midday on a sunday ;-)

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

## We haven't put in wireless yet...

...since all our computers are in two rooms that are back to back and the only laptop is my son&#039;s old P-166 beast that he mostly only uses for writing. But it looks like we&#039;ll be rearranging things soon, so wireless is surely in our future. I&#039;m not sure I even *want* a laptop, though. &#060;shudder&#062;computing in bed&#060;/shudder&#062;<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
19,082

## RE: We haven't put in wireless yet...

Wireless is great - there&#039;s something very cool about being able to check your email from anywhere in the house without a 50ft CAT5 cable trailing you round. I got mine mostly so I could split the cost of my broadband bill with the geeks next door, but it&#039;s proved to be far cooler than just \$40 or so a month off my bill. <BR><BR>Bit of a pain to set up the multiple routers though - I wanted to have a fairly open DMZ with a 2k3 box sitting in it serving stuff and running bots, then another closed zone behind another router where the wireless could live. took a couple of days to tweak it into shape.

#### Posting Permissions

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