In JavaScript, the double and int types are all viewed as number objects, so either typeof 1 or typeof 1.0 returns number. So we can let the JavaScript explain the engine inside out without having to go to the int or double type.
• If you want to convert number to string, you can use the ToString () method of number (like (1). ToString () parentheses must or 1. ToString () spaces must otherwise compile an error, if the variable is not required, Or you can call the string () function, both of which automatically invoke the Numbertostring () inside the interpreter engine, or call other functions based on the system, basically similar.
• If you want to convert a string to number, you can use the number () function, which automatically determines whether the string is an integer or a floating-point number, and then uses the corresponding data type internally, and you can use global functions parseint () and parsefloat (). They make the transition according to your request. Similarly, they use internal functions such as stringtonumber,stringtoint and so on to explain the engine's internal mechanism.
• If the double is converted to int, you must use the Math.floor () function (truncate rounding) or Math.Round () (rounded)
int to double, without considering any problems, directly to the int as a double
Note: The number, String function is a special function, in the JS engine, he will automatically judge whether it is a constructor call or a normal call, so you can use the New keyword, can also be called as a function directly.
About JS reference Manual, Microsoft has a CHM is very good, the Guide, API reference all have, is the Windows scripting technology, Chinese version, I downloaded on MSDN. And about JS explain engine, I refer to Netscape's SpiderMonkey, now by the Mozilla organization maintenance
The code is as follows |
Copy Code |
for (i=0;i<this.all.length;i++) { Vtotal+=number (This.all[i]. Value);
} |
The summation of the numbers
In JS, you can use the parseint function directly to convert
parseint (String): The function resolves from the beginning of a string and returns an integer.
Example:
The code is as follows |
Copy Code |
parseint (' 123 '): Return to 123 (int); parseint (' 1234xxx '): Return to 1234 (int); |
If the number is not resolved, the value of a Nan is returned, which can be detected using the isNaN () function;
Example:
The code is as follows |
Copy Code |
var i = parseint (' abc '); if (isNaN (i)) { Alert (' NaN value '); } parseint ("1234blue"); Returns 1234 parseint ("0xA"); Returns 10 parseint ("22.5"); Returns 22 parseint ("Blue"); Returns NaN
|
Some examples are as follows:
The code is as follows |
Copy Code |
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.
Examples are as follows:
The code is as follows |
Copy Code |
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 accept the cardinality 10 so that the octal value will not be obtained by accident. 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:
The same parsefloat function converts a string into a floating-point number.
Example: parsefloat (' 31.24abc '): return 31.24;
JS number converted to string
Converts a string to a number and uses the ToString method of the String class
Example:
The code is as follows |
Copy Code |
var i = 10; var s = i.tostring (); Alert (typeof s); Will output String |
The code is as follows:
The code is as follows |
Copy Code |
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 |
the difference between a JS number and a string
The addition of a
JS number to the string is a + symbol, so whether the addition or the string is connected depends on the type of the variable.
Example:
var a = ' abc ' + ' xyz ';//a values are: ABCXYZ, strings and strings are connected
var a = ten + 5;//a value is: 15, the number is plus
var a = ' abc ' + 10; The value of a is: ABC10, string and number, automatically converts 10 to a string
var a = ' abc ' + + ' CD ';//a value is: ABC1020CD
var a = + + ' abc ' + ' CD '; The value of a is: 30ABCCD, you can digitally add the first number plus, and then connect to the