Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Turning a string var to num

    I have a simple problem. I need the variables that I am collecting to add as numbers, but instead they are concatenating as strings. How can I make them numbers?

    Code:
    function disp_prompt(){
    	var num1=prompt("Please enter a number","");
    	var num2=prompt("Please enter another number","");
    	parseInt(num1);
    	parseInt(num2);
    	var sum=num1+num2;
    	document.write("You entered: ",num1," and ",num2,"<br>");
    	document.write("Sum: ",sum);
    As you can see I tried parseInt(), but that doesn't seem to work. Thanks.

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    Code:
     
     parseInt(num1);
    parseInt(num2);
    var sum=num1+num2;
    should be
    Code:
     
    var sum = parseInt(num1) + parseInt(num2);
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender


  •  

    Posting Permissions

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