1

i have:

var str="100px";
var number = str.split("px");
number = number[0];

var km = "100px";
var numberk = km.split("px");
numberk = numberk[0];

var gim = numberk+100;
var kim = number+100;
var fim = number+numberk;
document.write(gim+'<br>'+kim+'<br>'+jim+'<br>');

i would be thankfull if someone could me answere why the result are added like string rather than nummerical number in javascript i have used the isNaN(); function which shows this as a legal number. So how can this problem be solved.

thanks.

1
  • I seem to remember reading that jQuery can be used to add numbers... Commented Dec 27, 2011 at 17:02

6 Answers 6

2

You could use the parseInt function in order to convert the string returned when spliting into integer:

number = parseInt(number[0], 10);
numberk = parseInt(numberk[0], 10);

Now the 2 variables are integers and you could perform integer operations on them.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to put parseInt() around each number before you use it. In fact, you could do this without removing the "px".

 gim = parseInt(km) + 100;

Comments

1

simplest way to do this, you don't need to use split.

var str="150px";
var str1 = (parseInt(str)+100)+"px";
alert(str1);

OUTPUT:

200px

fiddle : http://jsfiddle.net/Kk3HK/1/

Comments

0

use parseInt()

var number = parseInt(str, 10);
var numberk = parseInt(km, 10);

Comments

0

Use parseInt to convert the string to a number.

var str = "100px";
var number = parseInt(str, 10);

parseInt stops when it finds the first non-number character, so you don't even need to remove the "px".

Comments

0

Wrap the numbers in parseInt().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.