How to convert javascript strings to numbers

Source: Internet
Author: User

If you use intval directly in php to convert strings to numbers, how can you convert strings to numbers in js, the following describes how to convert javascript strings to numbers. For more information, see.

In JavaScript, both the Double and Int types are regarded as Number objects. Therefore, both typeof 1 and typeof 1.0 return number. In this way, we don't need to worry about whether it is Int or Double type, so that the JavaScript interpretation engine can handle it internally.

• If you want to convert Number to String, you can use the toString () method of Number, such as (1 ). toString () brackets must be 1. the toString () space is required. Otherwise, compilation errors may occur. If it is a variable, no space is required.) or the String () function is called. Both methods automatically call the NumberToString () in the interpretation engine (), or call other functions in the hexadecimal format.
• If you want to convert a String to a 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, you can also use the global functions parseInt () and parseFloat () to convert them according to your requirements. Similarly, they use StringToNumber, StringToInt, and other internal functions in the internal mechanism of the interpretation engine.
• If the value is Double to Int, you must use the Math. floor () function (take an integer at the end of the truncation) or Math. round () (rounding)
• If Int is converted to a Double type, Int Is directly treated as a Double type without consideration.

Note: Number and String functions are special functions. In the JS engine, they will automatically determine whether they are called as constructors or normal calls. Therefore, they can use the new keyword, it can also be called directly as a function.
For the JS reference manual, Microsoft has a very good CHM, which is available in both the guide and API reference. It is a Windows Script technology and a Chinese version. I downloaded it on MSDN. For details about the JS interpretation engine, I refer to the Spidermonkey of Netscape, which is now maintained by Mozilla.
 

The Code is as follows: Copy code

For (I = 0; I <this. all. length; I ++ ){
VTotal + = Number (this. all [I]. Value );

}

The result is the accumulation of numbers.

In js, you can directly use the parseInt function for conversion.

ParseInt (string): The function is parsed from the string and returns an integer.
Example:

The Code is as follows: Copy code
ParseInt ('20140901'): returns 123 (int );
ParseInt ('1234xxx'): 1234 (int) is returned );

If no number is parsed, a NaN value is returned and 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 ("1234 blue"); // 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 ("1234 blue"); // returns 1234
ParseInt ("0xA"); // returns 10
ParseInt ("22.5"); // returns 22
ParseInt ("blue"); // returns NaN

The parseInt () method has another base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to an integer. The base is specified by the second parameter of the parseInt () method,

Example:

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 the leading 0, it is best to accept the base number 10 so that the octal value will not be obtained unmeasured. For example:
 
ParseInt ("010"); // returns 8
ParseInt ("010", 8); // returns 8
ParseInt ("010", 10); // returns 10

The parseFloat () method is similar to the parseInt () method.
Another difference between the parseFloat () method is that the string must represent a floating point number in decimal form, and parseFloat () has no base mode.
The following is an example of using parseFloat:

The same parseFloat function converts a string to a floating point number.
Example: parseFloat ('31. 24abc'): Return 31.24;

Convert JavaScript numbers to strings

To convert a String to a number, use 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); // returns String

The Code is as follows:

The Code is as follows: Copy code

ParseFloat ("1234 blue"); // 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


Differences between js numbers and strings

The addition of numbers in js and the connection between strings are both + symbols. Therefore, whether it is a plus or a string connection depends on the type of the variable.
Example:
Var a = 'abc' + 'xyz'; // The value of a is abcxyz, and the string is connected to the string.
Var a = 10 + 5; // The value of a is: 15, and the number is plus.
Var a = 'abc' + 10; // The value of a is abc10. the string and number are automatically converted to a string.
Var a = 'abc' + 10 + 20 + 'cd'; // The value of a is abc1020cd.
Var a = 10 + 20 + 'abc' + 'cd'; // The value of a is 30 abccd. You can add a number first and then connect it.

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.