JavaScript----Number Conversion string & string forward number

Source: Internet
Author: User
Tags type casting

Convert numbers to Strings

1.toString () function

var num=12345;var s=num.tostring (); function changetostr (num) {return num.tostring ();}

2. Using the weak type features of JS

var num=123;var s=num+ "";

Convert strings to Numbers

1. Conversion functions

JavaScript provides the parseint () and parsefloat () functions

  Note: Only these methods are called for the string type, and the two functions work correctly, and the other types return Nan.

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

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

parseint ("AF", 16); Returns 175PARSEINT ("10", 2); Returns 2PARSEINT ("10", 8); Returns 8PARSEINT ("10", 10); Returns 10

Parsefloat () Converts a string to a floating-point number in decimal notation.

Parsefloat ("1234blue"); Returns 1234.0parseFloat ("0xA"); Returns Nanparsefloat ("22.5"); Returns 22.5parseFloat ("22.34.5"); Returns 22.34parseFloat ("0908"); Returns 908parseFloat ("Blue"); Returns NaN

2. Casting

You can also use the coercion type conversion (type casting) to handle the type of the converted value. A specific value can be accessed using a coercion type conversion, even if it is of a different type.

The 3 mandatory type conversions available in ECMAScript are as follows: Boolean (value)-converts the given value to a Boolean type;

Number (value)-converts the given value to a digit (which can be an integer or a floating point);

String (value)-converts the given value to a string.

Using one of these three functions to convert the value, a new value is created that holds the value converted directly from the original value. This can cause unintended consequences. The Boolean () function returns True when the value to be converted is a string of at least one character, a non-0 number, or an object (this is discussed in the next section). If the value is an empty string, the number 0, undefined, or null, it returns FALSE.

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

Boolean (""); False–empty Stringboolean ("HI"); True–non-empty Stringboolean (100); True–non-zero Numberboolean (NULL); False-nullboolean (0); False-zeroboolean (New Object ()); True–object

The coercion type conversion of number () is similar to the parseint () and parsefloat () methods, except that it transforms the entire value, not the partial value

Number (FALSE) 0 number (TRUE) 1 number (undefined) NaN number (NULL) 0 number ("5.5") 5.5 Number ("5.6.7") ) Nan Number (new Object ()) Nan Number (100) 100

3. Using the weak type features of JS

<script> var str= ' 12.345 '; var x = str-0; x = x*1; </script>

The last one is not recommended!

Some examples are from the network ~

JavaScript----Number Conversion string & string forward number

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.