www.webdeveloper.com
Results 1 to 11 of 11

Thread: add two numbers in javascript

  1. #1
    Join Date
    Apr 2008
    Posts
    52

    add two numbers in javascript

    hello friends,
    how to add following two numbers in javascript
    034,034 pls help me.

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    What have you tried to do?

    Eric

  3. #3
    Join Date
    Mar 2008
    Posts
    16
    usually i would say '+', but it looks like you have strings, to do this:

    var a = '034';
    var b = '034';

    var c = parseInt(a)+parseInt(b);
    alert(c); //should alert 68

  4. #4
    Join Date
    Apr 2008
    Posts
    52
    padre i didnt get 68. i got 56.

  5. #5
    Join Date
    Apr 2008
    Posts
    52
    Eric,
    i tried to do a simple addition but i got 56 how is it? pls

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415
    You're trying to add two octal numbers with the leading '0'
    Try parseInt(a,10) instead.
    See: http://www.w3schools.com/jsref/jsref_parseInt.asp

  7. #7
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,774
    You could use the Number() function too:
    Code:
    var a = "034";
    var b = "034";
    
    alert( Number(a) + Number(b) );
    That would eliminate any number base confusion on the browser's part, because the Number() function always assumes the number is base-10, unless the string passed to the number function starts with "0x". It always returns a base 10 number.

  8. #8
    Join Date
    Apr 2008
    Posts
    52
    toicontien i got the answer thank u lot...........

  9. #9
    Join Date
    May 2012
    Posts
    1
    You can use parseInt() function also
    var c = parseInt(a,10)+parseInt(b,10);
    alert(c); //should alert 68

  10. #10
    Join Date
    Mar 2014
    Posts
    1
    function addingNumbers(){
    var firstNumber=Number(place where it is saved);
    var secondNumber=Number(place where the number is saved);
    var result=firstNumber+secondNumber;
    alert("the end result is : "+result);
    }

  11. #11
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415
    Is that last post a question or a statement?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles