Go language string, int, int64, float type conversion between a method of Go string, int, int64, float type conversion method between

(Content messy, later finishing!)

Original link: https://www.cnblogs.com/fanbi/p/10928965.html

Go type conversion method between language string, int, int64, float

Text 1

(1) int turn string

1
2
s := strconv.Itoa(i)
等价于s := strconv.FormatInt(int64(i), 10)

(2) int64 turn string

1
2
i := int64(123)
s := strconv.FormatInt(i, 10)

The second parameter as the base, alternatively 2 to 36

Note: For the unsigned integer may be usedFormatUint(i uint64, base int)

(3) string transfected int

1
i, err := strconv.Atoi(s)

(4) string rotation int64

1
i, err := strconv.ParseInt(s, 10, 64)

The second parameter as the base (2 to 36), the third parameter bits represent the size of the desired conversion result type, the value may be 0, 8, 16, 32 and 64, respectively int, int8, int16, int32 int64 and

(5) float-related

float switch string:

1
2
v := 3.1415926535
s1 := strconv.FormatFloat(v, 'E' , -1, 32) //float32s2 := strconv.FormatFloat(v, 'E', -1, 64)//float64

Meaning specific function prototypes and parameters can be viewed: https://golang.org/pkg/strconv/#FormatFloat

string turn float:

1
2
3
s := "3.1415926535"
v1, err := strconv.ParseFloat(v, 32)
v2, err := strconv.ParseFloat(v, 64)

 PS: go Language string, int, int64 interchangeable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//string到int
int,err:=strconv.Atoi(string)
//string到int64
int64, err := strconv.ParseInt(string, 10, 64)
//int到string
string:=strconv.Itoa(int)
//int64到string
string:=strconv.FormatInt(int64,10)
//string到float32(float64)
float,err := strconv.ParseFloat(string,32/64)
//float到string
string := strconv.FormatFloat(float32, 'E' , -1, 32)
string := strconv.FormatFloat(float64, 'E' , -1, 64)
// 'b' (-ddddp±ddd,二进制指数)
// 'e' (-d.dddde±dd,十进制指数)
// 'E' (-d.ddddE±dd,十进制指数)
// 'f' (-ddd.dddd,没有指数)
// 'g' ('e':大指数,'f':其它情况)
// 'G' ('E':大指数,'f':其它情况)

Text 1

(1) int turn string

1
2
s := strconv.Itoa(i)
等价于s := strconv.FormatInt(int64(i), 10)

(2) int64 turn string

1
2
i := int64(123)
s := strconv.FormatInt(i, 10)

The second parameter as the base, alternatively 2 to 36

Note: For the unsigned integer may be usedFormatUint(i uint64, base int)

(3) string transfected int

1
i, err := strconv.Atoi(s)

(4) string rotation int64

1
i, err := strconv.ParseInt(s, 10, 64)

The second parameter as the base (2 to 36), the third parameter bits represent the size of the desired conversion result type, the value may be 0, 8, 16, 32 and 64, respectively int, int8, int16, int32 int64 and

(5) float-related

float switch string:

1
2
v := 3.1415926535
s1 := strconv.FormatFloat(v, 'E' , -1, 32) //float32s2 := strconv.FormatFloat(v, 'E', -1, 64)//float64

Meaning specific function prototypes and parameters can be viewed: https://golang.org/pkg/strconv/#FormatFloat

string turn float:

1
2
3
s := "3.1415926535"
v1, err := strconv.ParseFloat(v, 32)
v2, err := strconv.ParseFloat(v, 64)

 PS: go Language string, int, int64 interchangeable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//string到int
int,err:=strconv.Atoi(string)
//string到int64
int64, err := strconv.ParseInt(string, 10, 64)
//int到string
string:=strconv.Itoa(int)
//int64到string
string:=strconv.FormatInt(int64,10)
//string到float32(float64)
float,err := strconv.ParseFloat(string,32/64)
//float到string
string := strconv.FormatFloat(float32, 'E' , -1, 32)
string := strconv.FormatFloat(float64, 'E' , -1, 64)
// 'b' (-ddddp±ddd,二进制指数)
// 'e' (-d.dddde±dd,十进制指数)
// 'E' (-d.ddddE±dd,十进制指数)
// 'f' (-ddd.dddd,没有指数)
// 'g' ('e':大指数,'f':其它情况)
// 'G' ('E':大指数,'f':其它情况)

Guess you like

Origin www.cnblogs.com/skzxc/p/11956309.html