Adding two numbers in JavaScript incorrectly

后端 未结 6 1099
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 14:05
Global.alert(\"base: \" + base + \", upfront: \" + upfront + \", both: \" + (base + upfront));

The code above outputs something like:



        
相关标签:
6条回答
  • 2020-12-03 14:40

    Simple example:

     1 +1 == 2
    "1"+1 == "11"
    "1"*1 + 1 == 2
    

    Ways to turn a string into a number:

    • parseInt(str)
    • parseInt(str,10)
    • parseFloat(str)
    • +str
    • str*1
    • str-0
    • str<<0
    • Number(str)

    And here are some of the consequences:
    (source: phrogz.net)

    Number(str) has the same behavior as str*1, but requires a function call.

    I personally use *1 as it is short to type, but still stands out (unlike the unary +), and either gives me what the user typed or fails completely. I only use parseInt() when I know that there will be non-numeric content at the end to ignore, or when I need to parse a non-base-10 string.

    You can test the performance of these in your browser at my example page.

    0 讨论(0)
  • http://jsperf.com/number-vs-parseint-vs-plus/3

    That might also be of interest to you. It is just a performance comparison of the methods already mentioned here.

    0 讨论(0)
  • 2020-12-03 14:57

    I don't know why the brackets aren't helping you out.
    If I try

    var base = 500;
    var upfront = 100;
    alert("base: " + base + ", upfront: " + upfront + ", both: " + (base + upfront))
    

    I do get 600 as the answer, so it could be there is something going on in the Global.alert function?

    One of the mistakes of the language design is that + is both an addition operator and a concatenation operator. Coupled with the fact that it is loosely typed and will cast implicitly means that it can give you some nasty surprises unless you take steps to ensure that you really are adding numbers and not concatenating strings. In this case, it is treating your base + upfront as strings and therefore concatenating.

    Anyway, the way around it could be to have (base - upfront*-1) instead.

    0 讨论(0)
  • 2020-12-03 14:58

    Try

    Global.alert(
        "base: " + base + ", upfront: " + upfront + ", both: " + 
        (parseInt(base,10) + parseInt(upfront,10))
    );
    

    The 10 specifies base 10, otherwise the chance of the value being parsed as octal exists.

    0 讨论(0)
  • 2020-12-03 14:59

    It's handling it as a string. You need to do your math before the string. Example:

    base + upfront + ' string' 
    

    would return "15036 string".

    string + base + upfront
    

    would return string 1500036 as you are seeing now.

    Or use parseInt().

    0 讨论(0)
  • 2020-12-03 15:01

    This might happen because they are strings. Try parsing them:

    Global.alert(
        "base: " + base + ", upfront: " + upfront + ", both: " + 
        (parseInt(base) + parseInt(upfront))
    );
    

    If those numbers are decimal you will need the parseFloat method instead.

    0 讨论(0)
提交回复
热议问题