The parseInt number in js, and detailed description of sum usage

Source: Internet
Author: User

The parseInt () function can parse a string and return an integer. You can also add or subtract an integer. Next I will introduce the parseInt number in js and explain the sum usage.

We all know what the javascript parseInt function is.

But you know

What is returned by parseInt ("07?
What is returned by parseInt ("08?

The correct answer is:

ParseInt ("07") returns 8
ParseInt ("08") returns 0

You know where the problem is?
In fact, you may not have thought about this issue.
When using the javascript parseInt function,

ParseInt ("08") or parseInt ("09") returns 0,

ParseInt ("01")... parseInt ("07") is correct.
 
Why?

It was hard to understand at first, and later I found that the cause of this problem was that when there was "0" in front,

The debugger of javascript considers this to be an octal number,
 
"08" and "09" are not legal Octal numbers,

This problem occurs, but parseFloat does not.

The parseInt () function in javascript reference is described as follows:

ParseInt Method

Returns an integer converted from a string. Converts a string to an integer.

ParseInt (numString, [radix])

The parseInt parameter radix:

Optional. Between 2 and 36, it indicates the hexadecimal value of the number saved by numString. If this parameter is not provided, the string prefixed with '0x 'is treated as hexadecimal, and the string prefixed with '0' is treated as octal. All other strings are treated as decimal.
Therefore, if you use parseInt ("08", 10) or parseInt ("09", 10 ),
Notify the javascript interpreter to use decimal to parse and obtain the number you want.

Appendix:

The Code is as follows: Copy code

ParseInt ("abc") // return NaN.
ParseInt ("12abc") // return 12.

Example

The Code is as follows: Copy code

<Title> Use parseInt to parse numbers and sum them </title>
<Style>
Body {font: 12px/1.5 arial; text-align: center ;}
. F-text {width: 50px; border: 1px solid # ccc; background: # f0f0f0; font-family: inherit; padding: 3px ;}
Span {color: green; padding-right: 10px; font-weight: 700 ;}
Strong {padding: 0 10px ;}
</Style>
<Script type = "text/javascript">
Window. onload = function ()
{
Var aInput = document. getElementsByTagName ("input ");
Var aSpan = document. getElementsByTagName ("span") [0];
Var I = 0;
 
For (I = 0; I <aInput. length-1; I ++)
{
AInput [I]. onkeyup = function ()
{
This. value = this. value. replace (/[^ d]/, "")
}
}
 
AInput [2]. onclick = function ()
{
(AInput [0]. value = "" | aInput [1]. value = "")?
Alert ("enter a number! "):
ASpan. innerHTML = parseInt (aInput [0]. value) + parseInt (aInput [1]. value );
}
};
</Script>
</Head>
<Body>
<Input type = "text" class = "f-text"/> <strong> + </strong> <input type = "text" class = "f-text"/> <strong >=</strong> <span>? </Span> <input type = "button" value = "sum"/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.