Object Type when comparing Javascript numbers

Source: Internet
Author: User

In js, the most common numeric data is used for comparison. However, if I directly perform normal comparison, problems may occur, next, I will introduce some considerations when comparing the size.

Use js to obtain the values of the two numeric input boxes on the page and compare the values. The result may be confusing, for example, the Code:

The Code is as follows: Copy code

Var v1 = $ ('# input1'). val ();
Var v2 = $ ('# input2'). val ();
Alert (v1> v2 );

For example, enter 10 in input1 and 2 in input2. 10 must be greater than 2, so the alert box above should pop up "true", but it actually gives "false. Why? There is no doubt that the number 10 is greater than 2. If it is compared as a string, the result is the opposite. The String type is obtained from the page.


Example

The Code is as follows: Copy code

<Script>
Alert ('000000'> '40'); // false because 1 <4
Alert (120> 40.00); // true
</Script>

 
In js scripts, we sometimes need to compare the sizes of two values, and both values are numbers,
 
For example

The Code is as follows: Copy code

Function getBigger (a, B ){

Return a> B? A: B;

}

If you explicitly want to compare numbers, you need to convert them to numbers, such as parseInt and parseFloat. Alternatively, you can use the subtraction operator, such as v1-v2> 0, in this case, js will convert the type before the operation.

Example

The Code is as follows: Copy code

<Html>
<Head>
<Script language = "javascript">
Function compare ()...{
If (parseInt (document. form1.a. value) <parseInt (document. form1. B. value ))...{
Alert ("a <B ");
} Else ...{
Alert ("a> B ");
}

}
</Script>
</Head>
<Form name = form1>
<Input name = a type = text/>
<Input name = B type = text/>
<Input type = button onclick = "compare ()" value = "compare"/>
</Form>
</Html>

ParseInt (cString) is an integer that is converted from a string cString that is not a null character. It is stopped when it encounters a decimal point or a character other than 0-9, for example, "-1234a ", "-1234.0" will return-1234; if none of the 0-9 characters except the first symbol, NaN will be returned, such as "-a" and "abc;

In addition, because addition operators support both numbers and strings, be careful when using them. For example, a + B. If one of them is a string type and the other is a number type, the other is converted to a string type before the computation. Therefore, the string link operation is actually performed, instead of the expected arithmetic operation. When it is determined that an arithmetic operation is required, parseInt is still used for type conversion for unknown parameters.

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.