adding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart

    adding


    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)


  • Lasse Reichstein Nielsen

    #2
    Re: adding

    "Stuart" <kffgtr@yytd.gf t> writes:
    [color=blue]
    > 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![/color]

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

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Svend Ezaki Tofte (DIKU)

      #3
      Re: adding

      On Wed, 5 Nov 2003, Stuart wrote:
      [color=blue]
      > 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![/color]

      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

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: adding

        "Svend Ezaki Tofte (DIKU)" <di030271@diku. dk> writes:
        [color=blue]
        > If I want to force conversion, and I'm lazy, I go:
        >
        > baz = 0 + foo + bar;[/color]

        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>
        [color=blue]
        > It's the same ideas as when you want to turn stuff into a string:
        >
        > baz = "" + foo + bar;[/color]

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

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Dr John Stockton

          #5
          Re: adding

          JRS: In article <Pine.LNX.4.58. 0311060627360.1 9360@brok.diku. dk>, seen
          in news:comp.lang. javascript, Svend Ezaki Tofte (DIKU)
          <di030271@diku. dk> posted at Thu, 6 Nov 2003 06:29:23 :-[color=blue]
          >On Wed, 5 Nov 2003, Stuart wrote:
          >[color=green]
          >> 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![/color]
          >
          >If I want to force conversion, and I'm lazy, I go:
          >
          >baz = 0 + foo + bar;[/color]

          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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

          Comment

          Working...