Javascript learning, javascript

Source: Internet
Author: User

Javascript learning, javascript
Number and String Conversion

In the previous blog, I wrote string cutting. Here I wrote down the conversion between strings and numbers.

Number

Simply convert a numeric string into an integer.
The Code is as follows:

Var num = "12345"; var num_a = Number (num) + 1; alert (num_a); // The value of num_a is: 12346

If we want to extract a number from the string, we need to use the string cut described in the previous blog. What if we don't cut it?

Var str = "iamalps1992"; var num = Number (str); alert (num); // the output of num is NaNnum = str. subString (7, 11); num = Number (num); alert (num); // output 1992

The following are the attributes of some objects of Number: (The table content is from W3Cschool)

Attribute Description
Constructor Returns a reference to the Number function that creates this object.
MAX_VALUE The maximum number that can be expressed.
MIN_VALUE The smallest number that can be expressed.
NaN Non-numeric value.
NEGATIVE_INFINITY Negative infinity. This value is returned when overflow occurs.
POSITIVE_INFINITY Positive infinity. This value is returned when overflow occurs.
ToString

After the number operation is completed, you may want to use a string because many operations are based on strings.

In particular, the id on my web page starts with a letter, but the numbers are arranged in an orderly manner.idAuto-increment 1. Then we can only take the numbers out.+1And then convert it to a string.

ToString () is a method of the Number object.

The method is simple:

NumberObject.toString(radix);
Parameters Description
Radix Optional. Indicates the base number of a number, so that 2 ~ An integer between 36. If this parameter is omitted, base 10 is used. Note that if this parameter is a value other than 10, the ECMAScript standard allows any value to be returned. Source: W3Cschool

The following is an example:

Var str = "iamalps1992"; var num = str. subString (7, 11); num = Number (num) + 1; str = str. subString (0, 7) + num. toString (); // If radix is not specified, the default value is 10 hexadecimal alert (str); // output: iamalps1993

In this case, it is relatively simple.

Number object Method
Method Description
ToString Converts a number to a string and uses the specified base number.
ToLocaleString Converts a number to a string in the format of a local number.
ToFixed Converts a number to a string. The result is a number of the specified digits after the decimal point.
ToExponential Converts an object value to an exponential notation.
ToPrecision Format the number to the specified length.
ValueOf Returns the basic numeric value of A Number object.

Source: W3Cschool

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.