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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a string to a number

Status
Not open for further replies.

LewisReeder

IS-IT--Management
Jul 18, 2005
38
US
I am creating a web application that requires the user to enter an amount for each day of the week. The numbers are entered into text boxes. I need to have a running total of all the numbers as they input each number. I am using the onchange event handler so each time they move off the boxes the total calculates and is displayed in a separate box. The problem I am having is that I am not able to convert the string to a number (1-3 digit number). I know this because when the total is displayed, each number is added as the next character in the string rather than creating a total. How can I convert my string to a number (with decimals)?
 
Here's another (shorthand) way to cast a string into a number:
Code:
<script type="text/javascript">
var myString1 = "123.45";
var myString2 = "13.5";
alert((myString1-0) + (myString2-0)); // 136.95
</script>
By subtracting zero from a string, Javascript converts the string into the appropriate number format for you.

Cheers,
Jeff


[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top