heldenbrau Posted August 29, 2013 Share Posted August 29, 2013 I have a variable which is 100 and I want to add 1000 to it making 1100 tbox$count.value = change$count+1000; It give the following 1001000 What do I need to do to make it display 1100? Quote Link to comment https://forums.phpfreaks.com/topic/281680-add-1000-to-100-to-display-1100/ Share on other sites More sharing options...
Solution kicken Posted August 29, 2013 Solution Share Posted August 29, 2013 You need to convert your change$count variable to a number before you can add it. A quick way to do this is to just prefix it with a +. tbox$count.value = (+change$count)+1000; Quote Link to comment https://forums.phpfreaks.com/topic/281680-add-1000-to-100-to-display-1100/#findComment-1447385 Share on other sites More sharing options...
heldenbrau Posted August 29, 2013 Author Share Posted August 29, 2013 Thanks loads, that worked. Should have asked here an hour ago instead of looking up all this parseInt stuff that didn't make any sense. Quote Link to comment https://forums.phpfreaks.com/topic/281680-add-1000-to-100-to-display-1100/#findComment-1447388 Share on other sites More sharing options...
.josh Posted August 30, 2013 Share Posted August 30, 2013 well, the + prefix is a shortcut. You could have alternatively done tbox$count.value = parseInt(change$count)+1000;or even tbox$count.value = Number(change$count)+1000;The overall takeaway here is that wherever you got change$count from (e.g. grabbing from some form field, receiving an ajax response, etc.), the value was stored as some other type (probably string). So you were basically doing "100"+1000 which javascript interprets as string concatenation. So there are ways (shown above) to convert the value to another variable type (type casting), in order to ensure javascript does what you expect it to do. It is actually one of those best things to check (you can use the typeof operator to check variable type) and/or convert variable type before performing operations, to ensure expected results. Quote Link to comment https://forums.phpfreaks.com/topic/281680-add-1000-to-100-to-display-1100/#findComment-1447439 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.