Numeric and string conversions
In a blog post I wrote a string cut here I write down, converting between strings and numbers.
Number
The simple thing is to convert a numeric string into an integer.
The code is as follows:
var"12345";varNumber1//num_a的值是: 12346
If we were to intercept a number in a string, we would need to use the string cut that was said in the previous blog post. What if I don't cut it?
var"iamalps1992";varNumber// num输出为 NaNnum = str.subString(711Number//num输出为 1992
The following are the properties of some of the objects of Number: (Table content from: W3cschool)
Properties |
Description |
Constructor |
Returns a reference to the number function that created this object |
Max_value |
The maximum number that can be represented. |
Min_value |
The smallest number that can be represented. |
NaN |
Non-numeric value. |
Negative_infinity |
Negative infinity, which returns this value when overflow. |
Positive_infinity |
Positive infinity, which returns this value when overflow. |
Tostring
When we have finished manipulating the numbers, we might prefer to have a string, because many operations are string-based.
In particular, the ID on my previous page was a letter that started with letters, but the numbers were ordered sequentially, and I wanted to increase it by 1 each time id
. Then you can just take the numbers out and +1
convert them to strings.
ToString () is a method of the Number object
The method is simple:
NumberObject.toString(radix);
Parameters |
Description |
Radix |
Optional. An integer that specifies the cardinality of the number that makes 2 ~ 36. If this argument is omitted, cardinality 10 is used. Note, however, that if the parameter is a value other than 10, the ECMAScript standard allows the implementation to return any value. Content Source: W3cschool |
Here is an example:
str"iamalps1992"str.subString(7111;strstr.subString(07//不写radix默认是10进制str//输出: iamalps1993
Roughly, it's easier.
Number Object method
Method |
Description |
Tostring |
Converts a number to a string, using the specified cardinality. |
toLocaleString |
Converts a number to a string, using the local number format order. |
ToFixed |
Converts a number to a string, the result of which has a specified number of digits after the decimal point. |
Toexponential |
Converts the value of an object to an exponential count method. |
Toprecision |
Formats the number as the specified length. |
ValueOf |
Returns the base numeric value of a number object. |
Content Source: W3cschool
JavaScript Learning-Numeric and string conversions