Simple Math

1. Member
Join Date
Dec 1969
Posts
78

## Simple Math

How can I add the values of these using ASP? I don&#039t need the values inserted to a database, just added up on the page.<BR><BR>&#060;input type="text" name="cost1" value=".99"&#062;<BR>&#060;input type="text" name="cost2" value="2.50"&#062;<BR>&#060;input type="text" name="cost3" value="1.89"&#062;<BR><BR>&#060;%str=cost1+cost2+c ost3%&#062;<BR><BR>&#060;%=str%&#062;

2. Steve Cimino Guest

## Ummm... ok

I think you have it, don&#039t you?<BR><BR>The only thing you&#039ll have to do is convert to a double.<BR><BR>&#060;%=CDbl(cost1) + CDbl(cost2) + CDbl(cost3)%&#062;<BR><BR>Note this will only work at the beginning of the page load. If the user changes the values, you&#039ll have to use JavaScript.

3. Member
Join Date
Dec 1969
Posts
78

## RE: Ummm... don't think so

The CDbl string didn&#039t work either. I get 00 for these two strings.<BR><BR>Do I need to declare something on the page?<BR>&#060;html&#062;<BR>&#060;body&#062;<BR>< BR>&#060;input type="text" name="cost1" value="1"&#062;<BR>&#060;input type="text" name="cost2" value="2"&#062;<BR>&#060;input type="text" name="cost3" value="3"&#062;<BR><BR>&#060;%str=Cdbl(cost1)+Cdbl (cost2)+Cdbl(cost3)%&#062;<BR>&#060;%=CDbl(cost1) + CDbl(cost2) + CDbl(cost3)%&#062; <BR>&#060;%=str%&#062;<BR><BR>&#060;/body&#062;<BR>&#060;/html&#062;

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

## You want this with *NO* ASP involved???

If so:<BR><BR>&#060;html&#062;<BR>&#060;body&#062;<BR ><BR>&#060;FORM Name="theForm"&#062;<BR>&#060;input type="text" name="cost1" value="1"&#062;<BR>&#060;input type="text" name="cost2" value="2"&#062;<BR>&#060;input type="text" name="cost3" value="3"&#062;<BR><BR>&#060;SCRIPT Language=JavaScript&#062;<BR>c1 = 1.0 * document.theForm.cost1.value;<BR>c2 = 1.0 * document.theForm.cost2.value;<BR>c3 = 1.0 * document.theForm.cost3.value;<BR>document.write ( c1 + c2 + c3 );<BR>&#060;/SCRIPT&#062;<BR>&#060;/FORM&#062;<BR>&#060;/body&#062;<BR>&#060;/html&#062;<BR><BR>That might not come out in pretty 2 digits after the decimal point form, in which case you&#039ll have to learn to format with JavaScript (ugh).<BR><BR>But if you want to do this in ASP, then show us the ASP code, not the HTML results.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
1,944

## RE: Ummm... don't think so

Hi,<BR>Look at this<BR><BR>&#060;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&#062;<BR><BR>&#060;html&#062;<BR>&#060;head&#0 62;<BR>&#060;title&#062;Untitled&#060;/title&#062;<BR>&#060;/head&#062;<BR>&#060;body&#062;<BR>&#060;form name="form1"&#062;<BR>&#060;input type="text" name="text1" value="10"&#062;<BR>&#060;input type="text" name="text2" value="10"&#062;<BR>&#060;input type="text" name="text3" value="10"&#062;<BR>&#060;/form&#062;<BR>&#060;script language="javascript"&#062;<BR>var Total=parseInt(document.form1.text1.value) + parseInt(document.form1.text2.value) + parseInt(document.form1.text3.value);<BR>document. write (Total)<BR>&#060;/script&#062;<BR><BR>&#060;/body&#062;<BR>&#060;/html&#062;<BR><BR>Sreenivas

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

...except that you might want to use parseFloat instead of parseInt if the numeric values aren&#039t always integers.<BR><BR>I cheated in my version and simply did <BR>&nbsp; &nbsp; &nbsp; 1.0 * xxx.value<BR>instead of <BR>&nbsp; &nbsp; &nbsp; parseFloat( xxx.value )<BR><BR>The difference between the two: parseFloat (and parseInt) will *ignore* trailing "trash". So if the form value was "23.7 elephants", parseFloat would just return 23.7 and no error. Doing the multiply-by-1.0 trick will give you NaN if there is trailing trash. So pick the one that works for you.<BR><BR>

7. Member
Join Date
Dec 1969
Posts
78

## Show results as user clicks Prices?

You guys are really helfpful. I am wondering from a comment made earlier in this thread whether there is a way to make the results automatically show up on the page as the user makes different selections.

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

## Yes, but...

Yes, but the result (the sum) needs to be displayed in some piece of the web page that is easily altered.<BR><BR>So why not simply another field in the same form? You can even make it a read-only field.<BR><BR>Thus:<BR><BR>&#060;html&#062; <BR>&#060;body&#062; <BR><BR>&#060;FORM Name="theForm"&#062; <BR>&#060;input type="text" name="cost1" value="1" onBlur="doTotal();"&#062; <BR>&#060;input type="text" name="cost2" value="2" onBlur="doTotal();"&#062; <BR>&#060;input type="text" name="cost3" value="3" onBlur="doTotal();"&#062; <BR><BR>&#060;input name="total" onFocus="this.form.cost1.focus();"&#062;<BR><BR>&# 060;SCRIPT Language=JavaScript&#062; <BR>function doTotal()<BR>{<BR>&nbsp; &nbsp; c1 = 1.0 * document.theForm.cost1.value; <BR>&nbsp; &nbsp; c2 = 1.0 * document.theForm.cost2.value; <BR>&nbsp; &nbsp; c3 = 1.0 * document.theForm.cost3.value; <BR>&nbsp; &nbsp; temp = "" + Math.round( ( c1 + c2 + c3 ) * 100 ); // pennies as a string<BR>&nbsp; &nbsp; if ( temp.length == 1 ) temp = "00" + temp;<BR>&nbsp; &nbsp; else if ( temp.length == 2 ) temp = "0" + temp;<BR>&nbsp; &nbsp; total = "\$" + temp.substring(0,temp.length-2) + "." + temp.substring(temp.length-2);<BR>&nbsp; &nbsp; document.theForm.total.value = total; <BR>}<BR>&#060;/SCRIPT&#062; <BR><BR>&#060;/FORM&#062; <BR>&#060;/body&#062; <BR>&#060;/html&#062; <BR><BR>****************<BR><BR>I *think* that will also format the total as \$ and cents properly. Oh, what the heck...let me go try it...<BR><BR>Yep...worked fine. Go for it!<BR><BR>

9. Member
Join Date
Dec 1969
Posts
78

## Excellent! Thanks.

Thanks.

#### Posting Permissions

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