What is the difference between parseInt() and Number()?

前端 未结 10 792
無奈伤痛
無奈伤痛 2020-11-22 12:02

How do parseInt() and Number() behave differently when converting strings to numbers?

相关标签:
10条回答
  • 2020-11-22 12:18

    I always use parseInt, but beware of leading zeroes that will force it into octal mode.

    0 讨论(0)
  • 2020-11-22 12:19
    typeof parseInt("123") => number
    typeof Number("123") => number
    typeof new Number("123") => object (Number primitive wrapper object)
    

    first two will give you better performance as it returns a primitive instead of an object.

    0 讨论(0)
  • 2020-11-22 12:19

    Its a good idea to stay away from parseInt and use Number and Math.round unless you need hex or octal. Both can use strings. Why stay away from it?

    parseInt(0.001, 10)
    0
    
    parseInt(-0.0000000001, 10)
    -1
    
    parseInt(0.0000000001, 10)
    1
    
    parseInt(4000000000000000000000, 10)
    4
    

    It completly butchers really large or really small numbers. Oddly enough it works normally if these inputs are a string.

    parseInt("-0.0000000001", 10)
    0
    
    parseInt("0.0000000001", 10)
    0
    
    parseInt("4000000000000000000000", 10)
    4e+21
    

    Instead of risking hard to find bugs with this and the other gotchas people mentioned, I would just avoid parseInt unless you need to parse something other than base 10. Number, Math.round, Math.foor, and .toFixed(0) can all do the same things parseInt can be used for without having these types of bugs.

    If you really want or need to use parseInt for some of it's other qualities, never use it to convert floats to ints.

    0 讨论(0)
  • 2020-11-22 12:27

    Well, they are semantically different, the Number constructor called as a function performs type conversion and parseInt performs parsing, e.g.:

    // parsing:
    parseInt("20px");       // 20
    parseInt("10100", 2);   // 20
    parseInt("2e1");        // 2
    
    // type conversion
    Number("20px");       // NaN
    Number("2e1");        // 20, exponential notation
    

    Also parseInt will ignore trailing characters that don't correspond with any digit of the currently used base.

    The Number constructor doesn't detect implicit octals, but can detect the explicit octal notation:

    Number("010");         // 10
    Number("0o10")         // 8, explicit octal
    
    parseInt("010");       // 8, implicit octal
    parseInt("010", 10);   // 10, decimal radix used
    

    And it can handle numbers in hexadecimal notation, just like parseInt:

    Number("0xF");   // 15
    parseInt("0xF"); //15
    

    In addition, a widely used construct to perform Numeric type conversion, is the Unary + Operator (p. 72), it is equivalent to using the Number constructor as a function:

    +"2e1";   // 20
    +"0xF";   // 15
    +"010";   // 10
    
    0 讨论(0)
  • 2020-11-22 12:29

    parseInt converts to a integer number, that is, it strips decimals. Number does not convert to integer.

    0 讨论(0)
  • 2020-11-22 12:30

    I found two links of performance compare among several ways of converting string to int.

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

    http://jsben.ch/#/zGJHM

    http://phrogz.net/js/string_to_number.html

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