Is parseInt() supposed to work like this?

前端 未结 7 1890
不思量自难忘°
不思量自难忘° 2020-12-17 23:15

If I write this script :

alert(parseInt(\"123blahblahblah456\"));

I get the alert with the value 123

Ideally, shouldn\'t th

相关标签:
7条回答
  • 2020-12-17 23:20

    Yes: parseInt() is absolutely meant to work like that; to quote the Mozilla Developer Network entry:

    The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN. If not NaN, the returned value will be the decimal integer representation of the first argument taken as a number in the specified radix (base). For example, a radix of 10 indicates to convert from a decimal number, 8 octal, 16 hexadecimal, and so on. For radices above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.

    If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

    It seems that parseInt() is explicitly expecting to take a string and will take the first sequence of numbers (until it encounters an invalid numerical character) and return that as a number of whatever base was specified in the radix parameter.

    Incidentally, to reduce errors when parsing the strings passed to parseInt() remember to use the radix parameter, for example:

    parseInt('123odod24',10); // for base-10
    parseInt('123odod24',16); // for base-16
    

    Reference:

    • parseInt() at the MDC.
    0 讨论(0)
  • 2020-12-17 23:22

    As far as I know the parseInt() method is trying to parse until it finds a character. So if you have

    • parseInt("123 Iam funny") it will return 123.
    • parseInt("whats up 4711") it will return NaN

    Some documentation you might be checking out :

    • http://www.devguru.com/technologies/ecmascript/quickref/parseint.html
    • http://www.bennadel.com/blog/2012-Exploring-Javascript-s-parseInt-And-parseFloat-Functions.htm
    0 讨论(0)
  • 2020-12-17 23:32

    yes ! like explain here : http://www.w3schools.com/jsref/jsref_parseInt.asp

    Tips and Notes

    Note: Only the first number in the string is returned!

    Note: Leading and trailing spaces are allowed.

    Note: If the first character cannot be converted to a number, parseInt() returns NaN.

    Example

    Parse different strings:

    <script type="text/javascript">
    
    document.write(parseInt("10") + "<br />");
    document.write(parseInt("10.33") + "<br />");
    document.write(parseInt("34 45 66") + "<br />");
    document.write(parseInt(" 60 ") + "<br />");
    document.write(parseInt("40 years") + "<br />");
    document.write(parseInt("He was 40") + "<br />");
    
    document.write("<br />");
    document.write(parseInt("10",10)+ "<br />");
    document.write(parseInt("010")+ "<br />");
    document.write(parseInt("10",8)+ "<br />");
    document.write(parseInt("0x10")+ "<br />");
    document.write(parseInt("10",16)+ "<br />");
    
    </script>
    

    The output of the code above will be:

    10
    10
    34
    60
    40
    NaN
    
    10
    8
    8
    16
    16
    
    0 讨论(0)
  • 2020-12-17 23:35

    parseInt attempts to parse the string until it finds a non-integer value, at which point it returns whatever it had.

    So if the string is:

    • 1234abcd - it returns 1234
    • 1a3f - it returns 1
    • a14883 - it returns NaN
    • 1.5 - it returns 1
    • -1.3a - it returns -1

    Same with parseFloat except that won't break on a .

    • 1234abcd - it returns 1234
    • 1a3f - it returns 1
    • a14883 - it returns NaN
    • 1.5 - it returns 1.5
    • -1.3a - it returns -1.3
    0 讨论(0)
  • 2020-12-17 23:35

    This is how it is suppose to work. It parse the string until it reaches a non numerical character.

    You might be interested in checking out the function isFinite(), it checks wether a string is a finite, legal number:

    isFinite("123"); // true
    isFinite("123a");// false
    

    However this will return true for empty string and whitespace. You can therefore improve this solution by writing

    mystring = "123";
    mystringb = " ";
    !isNaN(parseInt(mystring)) && isFinite(mystring); // true
    !isNaN(parseInt(mystringb)) && isFinite(mystringb); // false
    

    Based on these suggestions I'm sure you can build your own function that will ignore any string that contains nonnumerical characters.

    0 讨论(0)
  • 2020-12-17 23:42

    Yes, cf all the anwers. I'd like to add that this is why checking if certain value can be converted to a number, it's better to use Number or just +.

    Number("123blahblahblah456"); //=> NaN
    Number("123"); //=> 123
    +"97.221" //=> 97.221
    // if the conversion result needs to be an int
    Math.round(Number("123.4567")); //=> 123
    

    Be aware though that Number in some cases (unexpectely) returns 0.

    +null   //=> 0
    +"  "   //=> 0
    +""     //=> 0
    +false  //=> 0
    +[]     //=> 0
    
    0 讨论(0)
提交回复
热议问题