parseInt() parses number literals with exponent incorrectly

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

提交回复
热议问题