Jump to content


Photo

Add 1000 to 100 to display 1100


Best Answer kicken, 29 August 2013 - 04:38 PM

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;
Go to the full post


  • Please log in to reply
3 replies to this topic

#1 heldenbrau

heldenbrau

    Advanced Member

  • Members
  • PipPipPip
  • 159 posts

Posted 29 August 2013 - 04:31 PM

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?



#2 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,714 posts
  • LocationBonita, FL

Posted 29 August 2013 - 04:38 PM   Best Answer

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;

Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#3 heldenbrau

heldenbrau

    Advanced Member

  • Members
  • PipPipPip
  • 159 posts

Posted 29 August 2013 - 04:53 PM

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.



#4 .josh

.josh

    .josh

  • Staff Alumni
  • 14,830 posts

Posted 30 August 2013 - 08:11 AM

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.

Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com