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? Link to comment https://forums.phpfreaks.com/topic/281680-add-1000-to-100-to-display-1100/ Share on other sites More sharing options...
kicken Posted August 29, 2013 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; 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. 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. 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
Archived
This topic is now archived and is closed to further replies.