adding

Discussion in 'Javascript' started by Stuart, Nov 5, 2003.

  1. Stuart

    Stuart Guest

    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)
     
    Stuart, Nov 5, 2003
    #1
    1. Advertising

  2. "Stuart" <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 6, 2003
    #2
    1. Advertising

  3. 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
     
    Svend Ezaki Tofte (DIKU), Nov 6, 2003
    #3
  4. "Svend Ezaki Tofte (DIKU)" <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 6, 2003
    #4
  5. JRS: In article <>, seen
    in news:comp.lang.javascript, Svend Ezaki Tofte (DIKU)
    <> 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.
     
    Dr John Stockton, Nov 6, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul M.

    Adding to a list box in ASP .Net

    Paul M., Aug 16, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    6,750
    Paul M.
    Aug 22, 2003
  2. Curt Emich

    Adding VB.NET

    Curt Emich, Dec 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    609
    Mary Chipman
    Dec 16, 2003
  3. Jonas L

    VS is adding code when compiling

    Jonas L, Apr 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,040
    =?iso-8859-15?Q?Lasse_V=E5gs=E6ther_Karlsen?=
    Apr 1, 2004
  4. news.microsoft.com
    Replies:
    0
    Views:
    391
    news.microsoft.com
    May 3, 2004
  5. =?Utf-8?B?UGF0cmljaw==?=

    Need help adding a chart in VB.NET

    =?Utf-8?B?UGF0cmljaw==?=, May 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    12,816
    ian_taylor
    Apr 18, 2008
Loading...

Share This Page