JS string conversion to Digital method

Source: Internet
Author: User
Tags numeric numeric value type casting

isNaN Method |   Parseint Method | String Object

Apply to: Global Object
Requirements
Version 1
Returns the floating-point number converted from a string.

Parsefloat (numstring)
Required numstring parameter is a string containing a floating-point number.

Description
The Parsefloat method returns a numeric representation that is equal to the number saved in the numstring. Returns NaN (not a number) if the numstring prefix cannot be interpreted as a float.

Parsefloat ("abc")//Return NaN.
Parsefloat ("1.2ABC")//Return 1.2.
NaN can be detected using the isNaN method.

----------------------------------------------

parseint method
Please see
isNaN Method |   Parsefloat Method |   String Object | ValueOf method

Apply to: Global Object
Requirements
Version 1
Returns an integer converted from a string.

parseint (numstring, [radix])
Parameters
Numstring
Required option. The string to convert to a number.
Radix
Options available. The value between 2 and 36 that represents the numstring of the saved number. If not provided, a string prefixed with ' 0x ' is treated as hexadecimal, and a string prefixed with ' 0 ' is treated as a octal. All other strings are treated as decimal.
Description
The parseint method returns an integer equal to the numeric value saved in numstring. If the prefix of the numstring cannot be interpreted as an integer, it returns NaN (not a number).

parseint ("abc")//Return NaN.
parseint ("12ABC")//return 12.
NaN can be detected using the isNaN method.


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 with at least one character, a number other than 0 digits, or an object (which is discussed in the next section). 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>

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.