hello friends,
how to add following two numbers in javascript
034,034 pls help me.
Printable View
hello friends,
how to add following two numbers in javascript
034,034 pls help me.
What have you tried to do?
Eric
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
padre i didnt get 68. i got 56.
Eric,
i tried to do a simple addition but i got 56 how is it? pls
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
You could use the Number() function too:
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.Code:var a = "034";
var b = "034";
alert( Number(a) + Number(b) );
toicontien i got the answer thank u lot...........
You can use parseInt() function also
var c = parseInt(a,10)+parseInt(b,10);
alert(c); //should alert 68
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);
}
Is that last post a question or a statement?