parseInt() parses number literals with exponent incorrectly

后端 未结 6 1382
慢半拍i
慢半拍i 2021-01-17 13:48

I have just observed that the parseInt function doesn\'t take care about the decimals in case of integers (numbers containing the e character).

相关标签:
6条回答
  • 2021-01-17 13:53

    parseInt(-3.67394039744206e-15) === -3

    The parseInt function expects a string as the first argument. JavaScript will call toString method behind the scene if the argument is not a string. So the expression is evaluated as follows:

    (-3.67394039744206e-15).toString()
    // "-3.67394039744206e-15"
    parseInt("-3.67394039744206e-15")
    // -3
    

    -3.67394039744206e-15.toFixed(19) === -3.6739e-15

    This expression is parsed as:

    • Unary - operator
    • The number literal 3.67394039744206e-15
    • .toFixed() -- property accessor, property name and function invocation

    The way number literals are parsed is described here. Interestingly, +/- are not part of the number literal. So we have:

    // property accessor has higher precedence than unary - operator
    3.67394039744206e-15.toFixed(19)
    // "0.0000000000000036739"
    -"0.0000000000000036739"
    // -3.6739e-15
    

    Likewise for -3.67394039744206e-15.toFixed(2):

    3.67394039744206e-15.toFixed(2)
    // "0.00"
    -"0.00"
    // -0
    
    0 讨论(0)
  • 2021-01-17 13:57

    If the parsed string (stripped of +/- sign) contains any character that is not a radix digit (10 in your case), then a substring is created containing all the other characters before such character discarding those unrecognized characters.

    In the case of -3.67394039744206e-15, the conversion starts and the radix is determined as base 10 -> The conversion happens till it encounters '.' which is not a valid character in base 10 - Thus, effectively, the conversion happens for 3 which gives the value 3 and then the sign is applied, thus -3.

    For implementation logic - http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.2

    More Examples -

    alert(parseInt("2711e2", 16));
    alert(parseInt("2711e2", 10));

    TO note:

    The radix starts out at base 10.

    If the first character is a '0', it switches to base 8.

    If the next character is an 'x', it switches to base 16.

    0 讨论(0)
  • 2021-01-17 13:57

    It tries to parse strings to integers. My suspicion is that your floats are first getting casted to strings. Then rather than parsing the whole value then rounding, it uses a character by character parsing function and will stop when it gets to the first decimal point ignoring any decimal places or exponents.

    Some examples here http://www.w3schools.com/jsref/jsref_parseint.asp

    0 讨论(0)
  • 2021-01-17 14:10

    parseInt has the purpose of parsing a string and not a number:

    The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

    And parseInt calls the function ToString wherein all the non numerical characters are ignored.

    You can use Math.round, which also parses strings, and rounds a number to the nearest integer:

    Math.round("12.2e-2") === 0 //true
    
    0 讨论(0)
  • 2021-01-17 14:14

    I think the reason is parseInt converts the passed value to string by calling ToString which will return "-3.67394039744206e-15", then parses it so it will consider -3 and will return it.

    The mdn documentation

    The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN

    0 讨论(0)
  • 2021-01-17 14:16

    Math.round("12.2e-2") may round up or down based on the value. Hence may cause issues.

    new Number("3.2343e-10").toFixed(0) may solve the issue.

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