Js, jquery string number comparison size!

Source: Internet
Author: User
Tags numeric string to number type casting

Turn from: http://blog.sina.com.cn/s/blog_4b7b2fc501010xar.html


var defined variable should be a string, sometimes without a type conversion to compare, or less than 10 can also, if more than 10 will be wrong

Cases:
var hour_select_begin=$ (' #hour_select_begin option:selected '). Text (); var hour_select_end=$ (' #hour_select_end option:selected '). Text (); if (hour_select_begin>hour_select_end) {alert ("Start time cannot be greater than end time"); return false;}
At this point if the hour_select_begin>=10 will be judged to have problems;
Fix Method 1:
if (Eval_r (hour_select_begin) >eval_r (hour_select_end)) {alert ("Start time cannot be greater than end time"); return false;}
Fix Method 2:
Convert string to Number type: hour_select_begin= hour_select_begin-0;
hour_select_begin= hour_select_ end-0;


Add:

JS String Conversion number
There are mainly three kinds of methods

Conversion functions, coercion type conversion, the use of JS variable weak type conversion.

1. Conversion function:

JS provides the parseint () and parsefloat () two conversion functions. The former converts the value to an integer, and the latter converts the value to a floating-point number. These methods are invoked only on the string type, and the two functions are run correctly, and all other types are returned as Nan (not a number).



Some examples are as follows:

parseint ("1234blue"); Returns 1234
parseint ("0xA"); Returns 10
parseint ("22.5"); Returns 22
parseint ("Blue"); Returns NaN



The parseint () method also has a base pattern that converts binary, octal, hexadecimal, or any other string of strings into integers. The base is specified by the second parameter of the parseint () method, as shown in the following example:

parseint ("AF", 16); Returns 175
parseint ("10", 2); Returns 2
parseint ("10", 8); Returns 8
parseint ("10", 10); Returns 10
If the decimal number contains a leading 0, it is best to use cardinality 10 so that you do not accidentally get the octal value. For example:
parseint ("010"); Returns 8
parseint ("010", 8); Returns 8
parseint ("010", 10); Returns 10



The Parsefloat () method is similar to how the parseint () method is handled.
Another difference between using the Parsefloat () method is that the string must represent a floating-point number in decimal form, and parsefloat () does not have a base pattern.

The following is an example of using the Parsefloat () method:
Parsefloat ("1234blue"); Returns 1234.0
Parsefloat ("0xA"); Returns NaN
Parsefloat ("22.5"); Returns 22.5
Parsefloat ("22.34.5"); Returns 22.34
Parsefloat ("0908"); Returns 908
Parsefloat ("Blue"); Returns NaN

2. Force type conversions

You can also use coercion type conversion (type casting) to handle the type of the converted value. You can use coercion type conversions to access a specific value, even if it is of another type.
The 3 mandatory type conversions available in ECMAScript are as follows:
Boolean (value)--converts a given value to a Boolean;
Number (value)-Converts a given value to a digit (can be an integer or floating point);
String (value)--converts the given value to a string.
Converting a value with one of these three functions creates a new value that holds the value directly converted from the original value. This can have unintended consequences.
The Boolean () function returns True when the value to be converted is a string of at least one character, a number other than 0 digits, or an object. If the value is an empty string, number 0, undefined, or null, it returns FALSE.

You can use the following code snippet to test a Boolean type cast.

Boolean (""); False–empty string
Boolean ("Hi"); True–non-empty string
Boolean (100); True–non-zero number
Boolean (NULL); False-null
Boolean (0); False-zero
Boolean (New Object ()); True–object

The force type conversion of number () is similar to the parseint () and parsefloat () methods, except that it converts the entire value, not the partial value. Examples are as follows:

Usage results
Number (false) 0
Number (TRUE) 1
Number (undefined) NaN
Number (NULL) 0
Number ("5.5") 5.5
Number ("56") 56
Number ("5.6.7") NaN
Number (new Object ()) NaN
Number (100) 100

The last force type conversion method string () is the simplest and the example is as follows:

var S1 = String (null); "NULL"
var onull = null;
var s2 = onull.tostring (); Won ' t work, causes an error

3. Using JS variable weak type conversion

For a small example, a look, you will understand.
<script>
var str= ' 012.345 ';
var x = str-0;
x = x*1;
</script>

The above example uses the characteristics of the weak type of JS, only arithmetic operation, realize the type conversion of string to number, but this method is still not recommended




Convert to Number

ECMAScript provides two methods for converting a non-numeric original value into a number, namely parseint () and parsefloat ().
Note: These methods are called only for string type (except number) to correctly run Nan for other types.

For example: JS code

var iNum1 = parseint ("1234blue");//returns 1234 var iNum2 = parseint ("Oxa"); Returns var iNum3 = parseint ("22.5″)"; Returns var iNum4 = parseint ("Blue"); Returns NaN

The following other types of conversions

One, type conversion

1. Convert to String

The interesting thing about ECMAScript's Boolean values, numbers, and the original values of strings is that they are pseudo objects, which means they actually have properties and methods.
such as: JS code
var scolor = "Blue";

alert (scolor.length);//outputs "4″

All in all, 3 of the primary values Boolean values, numbers, and strings have the ToString () method. All objects defined by ECMAScript have the ToString () method, whether it is a pseudo object or a true object.

The Boolean ToString () method only outputs "true" or "false", and the result is determined by the value of the variable: JS code
var bfound = false;

Alert (bfound.tostring ());//outputs "false"

The ToString () method of the number type is special, it has two modes, the default mode and the base mode, the default mode, and the ToString () method simply outputs numeric values (whether integers, floating-point numbers, or scientific notation) with the corresponding string. JS Code

var iNum1 = 10;    var fNum2 = 10.0; Alert (inum1.tostring ()); Outputs "10″alert (fnum2.tostring ()); Outputs "10″

Using the base mode of the number type ToString () method, you can output numbers in different bases (in radix). JS Code

var inum = 10;  Alert (inum.tostring (2));  Outputs "1010″alert (inum.tostring (8)); Outputs "12″alert (inum.tostring (16)); Outputs "A"

2. Convert to Digital

ECMAScript provides two methods for converting a non-numeric original value into a number, namely parseint () and parsefloat ().
Note: These methods are called only for string type (except number) to correctly run Nan for other types.

For example: JS code

var iNum1 = parseint ("1234blue");//returns 1234 var iNum2 = parseint ("Oxa"); Returns var iNum3 = parseint ("22.5″)"; Returns var iNum4 = parseint ("Blue"); Returns NaN

The parseint () method also has a base pattern that converts binary, octal, hexadecimal, or any other string of strings into decimal integers. The second parameter specifies which system to parse. JS Code

var iNum1 = parseint ("AF");//returns 175 var iNum2 = parseint ("10″,2"); Returns 2 var iNum3 = parseint ("10″,8)"; Returns 8 var iNum4 = parseint ("10″,10)"; Returns 10

Note: If the decimal number contains a leading 0, it is best to use cardinality 10, otherwise the octal value is obtained. JS Code

var iNum1 = parseint ("010″"); Returns 8 var iNum2 = parseint ("010″,8)"; Returns 8 var iNum3 = parseint ("010″,10);//returns 10

The Parsefloat () method works like the parseint () method, viewing each character starting at position 0 until the first inactive character is found, and then converting the string before the character to a number. For this method, the first decimal point that appears is a valid character. If you use two decimal points, the second decimal point is considered invalid. Another difference in using this method is that the string must represent a floating-point number in decimal form. JS Code

var fNum1 = parsefloat ("1234blue"); Returns 1234.0 var fNum2 = parsefloat ("0xA"); Returns NaN var fNum3 = parsefloat ("22.5″)"; Returns 22.5 var fNum4 = parsefloat ("22.34.5″")//returns 22.34 var fNum5 = parsefloat ("0908″"); Returns Nan var fNum6 = parsefloat ("blue");//returns nan

3. Force type Conversions
The 3 mandatory type conversions available in ECMAScript are as follows:
(1). Boolean (value)
Converts a given value to a Boolean.
The Boolean () function returns True when the value to be converted is a string of at least one character, a number other than 0 digits, or an object. If the value is an empty string, number 0, undefined, or null, it returns FALSE.
such as: JS code

var b1 = Boolean ("");    False var b2 = Boolean ("Hi");//true var b3 = boolean;//true var b4 = Boolean (null );//false var b5 = boolean (0);//false var b6 = Boolean (new Object ());//true

(2). Number (value)
Converts a given value to a number (can be an integer or floating point count).
Remember that the parseint () and parsefloat () methods only convert the string before the first invalid character, so "4.5.6″ will be converted to" 4.5″. Force type conversion with number (), "4.5.6″ will return Nan, because the entire string value cannot be converted to numbers." If the string can be completely converted, number () will determine whether to call the parseint () method or call the Parsefloat () method.
such as: JS code

Number (false);//0 number (true);//1 number (undefined);//nan number (null);//0 Number ( "5.5″),//5.5 number (" 56″ "),//56 number (" 5.6.7″ "),//nan number (new Object ()),//nan number (100),//100

(3). String (value)
Converts a given value to a string.
The only difference from invoking the ToString () method is that coercion of a type conversion on a null or undefined value can generate a string without raising an error: JS code

var s1 = String (null);//"null" var onull = null; var s2 = onull.tostring ();//causes an error

Ii. type of reference
Reference types are usually called classes (class), which means that when you encounter a reference value, you are dealing with an object. ECMAScript defines an "object definition" that is logically equivalent to a class in another programming language.

1.Object class
All classes in ECMAScript are inherited by this class, and all the properties and methods in the object class appear in other classes (overwritten).

Properties of the object class:

(1). constructor--a reference (pointer) to the function that created the object. For the object class, the pointer points to the original object () function.

(2). prototype--a reference to the object's prototype of the object. For all classes, it returns an instance of Object objects by default. One, type conversion

1. Convert to String

The interesting thing about ECMAScript's Boolean values, numbers, and the original values of strings is that they are pseudo objects, which means they actually have properties and methods.
such as: JS code
var scolor = "Blue"; alert (scolor.length);//outputs "4″

All in all, 3 of the primary values Boolean values, numbers, and strings have the ToString () method. All objects defined by ECMAScript have the ToString () method, whether it is a pseudo object or a true object.

The Boolean ToString () method only outputs "true" or "false", and the result is determined by the value of the variable: JS code
var bfound = false; Alert (bfound.tostring ());//outputs "false"

The ToString () method of the number type is special, it has two modes, the default mode and the base mode, the default mode, and the ToString () method simply outputs numeric values (whether integers, floating-point numbers, or scientific notation) with the corresponding string.    JS code var INUM1 = 10;   var fNum2 = 10.0; Alert (inum1.tostring ()); Outputs "10″alert (fnum2.tostring ()); Outputs "10″

Using the base mode of the number type ToString () method, you can output numbers in different bases (in radix).   JS code var inum = 10;  Alert (inum.tostring (2));  Outputs "1010″alert (inum.tostring (8)); Outputs "12″alert (inum.tostring (16)); Outputs "A"

The parseint () method also has a base pattern that converts binary, octal, hexadecimal, or any other string of strings into decimal integers. The second parameter specifies which system to parse. JS code var INUM1 = parseint ("AF");//returns 175 var iNum2 = parseint ("10″,2"); Returns 2 var iNum3 = parseint ("10″,8)"; Returns 8 var iNum4 = parseint ("10″,10)"; Returns 10

Note: If the decimal number contains a leading 0, it is best to use cardinality 10, otherwise the octal value is obtained.  JS code var INUM1 = parseint ("010″"); Returns 8 var iNum2 = parseint ("010″,8)"; Returns 8 var iNum3 = parseint ("010″,10);//returns 10

The Parsefloat () method works like the parseint () method, viewing each character starting at position 0 until the first inactive character is found, and then converting the string before the character to a number. For this method, the first decimal point that appears is a valid character. If you use two decimal points, the second decimal point is considered invalid. Another difference in using this method is that the string must represent a floating-point number in decimal form. JS code var FNUM1 = parsefloat ("1234blue"); Returns 1234.0 var fNum2 = parsefloat ("0xA"); Returns NaN var fNum3 = parsefloat ("22.5″)";  Returns 22.5 var fNum4 = parsefloat ("22.34.5″);//returns 22.34 var fNum5 = parsefloat (" 0908″);//returns NaN var FNUM6 = parsefloat ("blue");//returns NaN

3. Coercion type conversion
The 3 types of coercion that are available in ECMAScript are as follows:
(1). Boolean (value)
converts a given value to a Boolean.
the Boolean () function returns True when the value to be converted is a string of at least one character, a number other than 0 digits, or an object. If the value is an empty string, number 0, undefined, or null, it returns FALSE.
such as: JS code var &NBSP;B1 = Boolean (""); false;   var  b2 = boolean ("Hi");//true   var  b3 = boolean (MB);//true   var &NBSP;B4 = Boolean (null);//false   var  b5 = Boolean (0);//false   var  b6 = Boolean (New & Nbsp;object ());//true  

(2). Number (value)
Converts a given value to a number (can be an integer or floating point count).
Remember that the parseint () and parsefloat () methods only convert the string before the first invalid character, so "4.5.6″ will be converted to" 4.5″. Force type conversion with number (), "4.5.6″ will return Nan, because the entire string value cannot be converted to numbers." If the string can be completely converted, number () will determine whether to call the parseint () method or call the Parsefloat () method.
such as: JS code number (FALSE),//0 number (true),//1 number (undefined),//nan number (null),//0 number ("5.5″"),//5.5 number (" 56″);//56 number ("5.6.7″);//nan Number (New Object ());//nan number (100);//100

(3). String (value)
Converts a given value to a string.
The only difference from invoking the ToString () method is that coercion of a type conversion on a null or undefined value can generate a string without raising an error: JS code
var S1 = String (null);//"null" var onull = null; var s2 = onull.tostring ();//causes an error

Ii. type of reference
Reference types are usually called classes (class), which means that when you encounter a reference value, you are dealing with an object. ECMAScript defines an "object definition" that is logically equivalent to a class in another programming language.

1.Object class
All classes in ECMAScript are inherited by this class, and all the properties and methods in the object class appear in other classes (overwritten).

Properties of the object class:

(1). constructor--a reference (pointer) to the function that created the object. For the object class, the pointer points to the original object () function.

(2). prototype--a reference to the object's prototype of the object. For all classes, it returns an instance of Object objects by default.

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.