What is the difference between parseInt() and Numb

2018-12-31 22:09发布

问题:

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

回答1:

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

Keep in mind that if parseInt detects a leading zero on the string, it will parse the number in octal base, this has changed on ECMAScript 5, the new version of the standard, but it will take a long time to get in browser implementations (it\'s an incompatibility with ECMAScript 3), also parseInt will ignore trailing characters that don\'t correspond with any digit of the currently used base.

The Number constructor doesn\'t detect octals:

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

But 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


回答2:

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.



回答3:

If you are looking for performance then probably best results you\'ll get with bitwise right shift \"10\">>0. Also multiply (\"10\" * 1) or not not (~~\"10\"). All of them are much faster of Number and parseInt. They even have \"feature\" returning 0 for not number argument. Here are Performance tests.



回答4:

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



回答5:

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



回答6:

One minor difference is what they convert of undefined or null,

Number() Or Number(null) // returns 0

while

parseInt() Or parseInt(null) // returns NaN


回答7:

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



回答8:

parseInt() ->Parses a number to specified redix. Number()->Converts the specified value to its numeric equivalent or NaN if it fails to do so.

Hence for converting some non-numeric value to number we should always use Number() function.

eg.

Number(\"\")//0
parseInt(\"\")//NaN

Number(\"123\")//123
parseInt(\"123\")//123

Number(\"123ac\") //NaN,as it is a non numeric string
parsInt(\"123ac\") //123,it parse decimal number outof string

Number(true)//1
parseInt(true) //NaN

there are various corner case to parseInt() functions as it does redix conversion, hence we should avoid using parseInt() function for coersion purposes. Now,to check weather the provided value is Numeric or not,we should use nativeisNaN() function



回答9:

Summary:

parseInt():

  • Takes a string as a first argument, the radix (An integer which is the base of a numeral system e.g. decimal 10 or binary 2) as a second argument
  • The function returns a integer number, if the first character cannot be converted to a number NaN will be returned.
  • If the parseInt() function encounters a non numerical value, it will cut off the rest of input string and only parse the part until the non numerical value.
  • If the radix is undefined or 0, JS will assume the following:
    • If the input string begins with \"0x\" or \"0X\", the radix is 16 (hexadecimal), the remainder of the string is parsed into a number.
    • If the input value begins with a 0 the radix can be either 8 (octal) or 10 (decimal). Which radix is chosen is depending on JS engine implementation. ES5 specifies that 10 should be used then. However, this is not supported by all browsers, therefore always specify radix if your numbers can begin with a 0.
    • If the input value begins with any number, the radix will be 10

Number():

  • The Number() constructor can convert any argument input into a number. If the Number() constructor cannot convert the input into a number, NaN will be returned.
  • The Number() constructor can also handle hexadecimal number, they have to start with 0x.

Example:

console.log(parseInt(\'0xF\', 16));  // 15

// z is no number, it will only evaluate 0xF, therefore 15 is logged
console.log(parseInt(\'0xFz123\', 16));

// because the radix is 10, A is considered a letter not a number (like in Hexadecimal)
// Therefore, A will be cut off the string and 10 is logged
console.log(parseInt(\'10A\', 10));  // 10

// first character isnot a number, therefore parseInt will return NaN
console.log(parseInt(\'a1213\', 10));


console.log(\'\\n\');


// start with 0X, therefore Number will interpret it as a hexadecimal value
console.log(Number(\'0x11\'));

// Cannot be converted to a number, NaN will be returned, notice that
// the number constructor will not cut off a non number part like parseInt does
console.log(Number(\'123A\'));

// scientific notation is allowed
console.log(Number(\'152e-1\'));  // 15.21