Click to See Complete Forum and Search --> : change string to int


michelle
04-18-2003, 11:04 AM
// I have two strings:
var first = "123px";
var second = "345px";

// I substring them to remove the "px"
var first = first.substring(0,first.length-2)
var second = second.substring(0,second.length-2)

// Now I want to get the result of them two together, namely 864
var mySum = first+second

// The problem is that first and second are being treated as strings and not integers, so the mySum is 123456 in this case.

Any thoughts?
// Michelle

khalidali63
04-18-2003, 11:10 AM
for this purpose you only need to do this

var first = parseInt("123px");
var second =parseInt("345px");

parseInt will by default parse it