Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Two Numbers 1

Status
Not open for further replies.

FontanaS

Programmer
May 1, 2001
357
US
Hi!

Yes my question is that simple!
I have the following javascript function in my asp web page -

<!--FUNCTION TO CALCULATE THE TOTAL AMOUNT-->
<script language="javascript">
function calctotal()
{
lvShip = document.forms['ordersnewform'].elements['OrderShipping'].value;
lvMisc = document.forms['ordersnewform'].elements['OrderMisc'].value;
lvTotal = (lvShip + lvMisc);
document.forms['ordersnewform'].elements['OrderTotals'].value = lvTotal;
}
</script>

I am having trouble with the line -
lvTotal = (lvShip + lvMisc);

lvShip and lvMisc are numbers (currency).
I want to add them together, but it is combining them.
Ex - 3+3 = 33. I want it to be 3+3 = 6.

THANKS!
 
if they will always be integer numbers, use parseInt. Otherwise, use parseFloat.

Code:
        lvShip = parseInt(document.forms['ordersnewform'].elements['OrderShipping'].value);
        lvMisc = parseInt(document.forms['ordersnewform'].elements['OrderMisc'].value);



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
FontanaS, there's one extra thing that should be noted when using parseInt - there are 2 parameters when calling that function. It will save you headache in the long run to pass the 2nd parameter each time you call the function:

Code:
lvShip = parseInt(document.forms['ordersnewform'].elements['OrderShipping'].value[!], 10[/!]);
lvMisc = parseInt(document.forms['ordersnewform'].elements['OrderMisc'].value[!], 10[/!]);

This ensures that the function returns a decimal number. The reason this is important is because numbers that are passed to the function with a leading 0 are treated as octal numbers. Try out this little test - the results are surprising when you don't pass the 2nd parameter:
Code:
alert(parseInt("09"));
alert(parseInt("09", 10));

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
spinning-dollar-sign.gif
word
spinning-dollar-sign.gif


-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top