Home > Mobile >  WeChat applet string is converted to figure how to do?
WeChat applet string is converted to figure how to do?

Time:10-11

WeChat applet string is converted to figure how to do? Many people want to know is converted to a digital how small string in program development, in general, we're going to use WeChat JavaScript to achieve the conversion in the small program, the following give you carefully introduces JavaScript string is converted to a digital method,

WeChat applet string is converted to figure how to do?

There are two main methods: conversion function, casts

Method one: conversion function

Js provides the parseInt () and parseFloat () two conversion functions, the former is to convert the value to an integer, which is to convert the value to a floating-point number, only call these methods of String type, these two functions can correct operation; For other types of returns are NaN (Nota Number),

Some examples are as follows:

The code is as follows:

ParseInt (1234 "blue");//returns 1234

ParseInt (" 0 xa ");//returns 10

The parseInt (" 22.5 ");//returns the 22

The parseInt (" blue ");//returns NaN

ParseInt () method and mode, can put the binary, octal, hexadecimal, or any other hexadecimal string into an integer, the base is the parseInt () method of the second parameter is specified, the sample is as follows:

The code is as follows:

ParseInt (" AF ", 16);//returns 175

ParseInt (" 10 ", 2);//returns 2

ParseInt (10, 8);//returns 8

ParseInt (10, 10);//returns 10

If decimal number include leading 0, so it is best to use base 10, so you don't accidentally get the value of the octal, for example:

The code is as follows:

The parseInt (" 010 ");//returns 8

The parseInt (" 010 ", 8);//returns 8

The parseInt (" 010 ", 10);//returns 10

ParseFloat () method and the parseInt () method to deal with in a similar way,

Use the parseFloat () method of another difference is that the string must be expressed as a decimal floating point Numbers, parseFloat () no base model,

Here is to use the parseFloat () method of example:

The code is as follows:

1234 parseFloat (" blue ");//returns 1234.0

The parseFloat (" 0 xa ");//returns NaN

The parseFloat (" 22.5 ");//returns 22.5

The parseFloat (" 22.34.5 ");//returns 22.34

The parseFloat (" 0908 ");//returns 908

The parseFloat (" blue ");//returns NaN

Method 2: casts

Can also use casts (type casting) processing conversion value types, using casts can access a specific value, even if it is another type of,

ECMAScript are available in three kinds of casting is as follows:

Boolean (value) - to convert the given value to a Boolean type;

Number (value) - converts the given value into digital (can be an integer or floating point);

String (value) - converts the given value into a String,

With one of the three function conversion value, will create a new value, deposited by the original value directly into the values of the which can cause unintended consequences,

When it comes to transformation of value is at least one character string, non-zero number or object (the next section will discuss this), Boolean () function returns true, if the value is an empty string, the number 0, undefined or null, it returns false,

Small programs can use the following code snippet test of the Boolean type casts,

The code is as follows:

Boolean (" ");//false - the 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

Number () casts and parseInt () and parseFloat () method to deal with in a similar way, just change it is the whole value, not part of the value, the sample is as follows:

The code is as follows:

Use the results

Number (false) 0

Number 1 (true)

Number (undefined) NaN

Number (null) 0

The Number (5.5
"5.5")
Number (" 56 ") 56

Number (5.6.7) NaN

NaN Number (new Object ())

Number (100) 100

Finally a method of casting the String () is the most simple, the sample is as follows:

The code is as follows:

Var s1=String (null);//"null"

Var oNull=null;

Var s2=oNull. ToString ();//won 't work, causes the an error
  • Related