Home > Web Front-end > JS Tutorial > body text

javascript parseInt function analysis (transfer)_Basic knowledge

WBOY
Release: 2016-05-16 18:55:05
Original
1075 people have browsed it

javascript's parseInt function
javascript's parseInt function, everyone knows what it does
But do you know how much does
parseInt("07") return?
parseInt("08 ") returns how many?
The correct answer is
parseInt("07") returns 8
parseInt("08") returns 0
Do you know where the problem is?
Actually, everyone may not have thought about this issue.
When using the parseInt function of JavaScript,
parseInt("08") or parseInt("09") actually returns 0,
and parseInt("01")...parseInt("07" ) are correct.
Why is this?
It was difficult to understand at first, but later I found out that the reason for this problem is that when there is "0" in front,
javascript's debugger will think it is an octal number,
and "08" and "09" " is not a legal octal number,
so it causes that problem, but parseFloat does not have this problem.
The parseInt() function is described in the JavaScript reference as follows:
parseInt method
Returns an integer converted from a string. Convert string to integer.
parseInt(numString, [radix])
Parameter radix of parseInt:
Optional. A value between 2 and 36 representing the base of the number held by numString. If not provided, strings prefixed with '0x' are treated as hexadecimal, and strings prefixed with '0' are treated as octal. All other strings are treated as decimal.
Therefore, if you use parseInt("08",10) or parseInt("09",10),
inform the javascript interpreter to use decimal to parse and you can get the number you want.
Attachment:
parseInt("abc") // Return NaN.
parseInt("12abc") // Returns 12.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!