parseInt() parses number literals with exponent incorrectly

后端 未结 6 1508
慢半拍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: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

提交回复
热议问题