Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > adding

Reply
Thread Tools

adding

 
 
Stuart
Guest
Posts: n/a
 
      11-05-2003

I am occasionally having problems with adding numbers together. Sometimes
(not always) 2 + 2 = 22, Obviousy this is a string issue. I am using the
following method to overcome this problem, however I feel it is a bit
contrived, and i am sure there is a proper way of doing it!

var asd = 2
var zxc=2
asd=asd-(-1*zxc)


 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      11-06-2003
"Stuart" <(E-Mail Removed)> writes:

> I am occasionally having problems with adding numbers together. Sometimes
> (not always) 2 + 2 = 22, Obviousy this is a string issue. I am using the
> following method to overcome this problem, however I feel it is a bit
> contrived, and i am sure there is a proper way of doing it!


<URL:http://jibbering.com/faq/#FAQ4_21>

/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
Svend Ezaki Tofte (DIKU)
Guest
Posts: n/a
 
      11-06-2003
On Wed, 5 Nov 2003, Stuart wrote:

> I am occasionally having problems with adding numbers together. Sometimes
> (not always) 2 + 2 = 22, Obviousy this is a string issue. I am using the
> following method to overcome this problem, however I feel it is a bit
> contrived, and i am sure there is a proper way of doing it!


If I want to force conversion, and I'm lazy, I go:

baz = 0 + foo + bar;

It's the same ideas as when you want to turn stuff into a string:

baz = "" + foo + bar;

But in general, 2 + 2 should /never/ give you 22. Unless you're pulling
the numbers from, say a input box (in which case you're pulling text!)

Regards,
Svend
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      11-06-2003
"Svend Ezaki Tofte (DIKU)" <(E-Mail Removed)> writes:

> If I want to force conversion, and I'm lazy, I go:
>
> baz = 0 + foo + bar;


That would be *too* lazy.

var foo="2", bar="2";
alert(0 + foo + bar);

This alerts "022". The type concversion favors strings over numbers,
so when you add a string to a number, the number is converted to a
string, not the other way around. The order doesn't matter.

Just do as the FAQ says The fastest method is the unary prefix
plus.
var baz = +foo + +bar;
<URL:http://jibbering.com/faq/#FAQ4_21>

> It's the same ideas as when you want to turn stuff into a string:
>
> baz = "" + foo + bar;


Which works, because the string wins, and evaluation is left to right.

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      11-06-2003
JRS: In article <(E-Mail Removed)> , seen
in news:comp.lang.javascript, Svend Ezaki Tofte (DIKU)
<(E-Mail Removed)> posted at Thu, 6 Nov 2003 06:29:23 :-
>On Wed, 5 Nov 2003, Stuart wrote:
>
>> I am occasionally having problems with adding numbers together. Sometimes
>> (not always) 2 + 2 = 22, Obviousy this is a string issue. I am using the
>> following method to overcome this problem, however I feel it is a bit
>> contrived, and i am sure there is a proper way of doing it!

>
>If I want to force conversion, and I'm lazy, I go:
>
>baz = 0 + foo + bar;


That's a nice easy method; the only fault, as far as I can see, is that
it does not work.

Tested solutions are safer.

Read the FAQ.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Adding Wireless to a Hard-Wired Home Office (w/Web & Email) Nick Knight Wireless Networking 2 01-06-2005 03:46 AM
Adding an Antenna Pigtail to a WiFi card Guy Smith Wireless Networking 0 12-31-2004 11:15 AM
Adding Linksys or Dlink into a Microsoft networking product config =?Utf-8?B?c2tsZWZmbg==?= Wireless Networking 0 12-03-2004 04:12 PM
RE: Adding laptop to a new network =?Utf-8?B?TWFyayBQ?= Wireless Networking 0 10-20-2004 07:45 PM
Adding Wireless acess point to existing network. Shawn Wireless Networking 1 10-17-2004 03:53 PM



Advertisments