Go语言string,int,int64 ,float之间类型转换方法 Go语言string,int,int64 ,float之间类型转换方法

(内容凌乱,日后整理!)

原文链接:https://www.cnblogs.com/fanbi/p/10928965.html

Go语言string,int,int64 ,float之间类型转换方法

1 正文

(1)int转string

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

(2)int64转string

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

第二个参数为基数,可选2~36

注:对于无符号整形,可以使用FormatUint(i uint64, base int)

(3)string转int

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

(4)string转int64

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

第二个参数为基数(2~36),第三个参数位大小表示期望转换的结果类型,其值可以为0, 8, 16, 32和64,分别对应 int, int8, int16, int32和int64

(5)float相关

float转string:

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

函数原型及参数含义具体可查看:https://golang.org/pkg/strconv/#FormatFloat

string转float:

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

 PS:go语言string、int、int64互相转换

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':其它情况)

1 正文

(1)int转string

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

(2)int64转string

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

第二个参数为基数,可选2~36

注:对于无符号整形,可以使用FormatUint(i uint64, base int)

(3)string转int

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

(4)string转int64

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

第二个参数为基数(2~36),第三个参数位大小表示期望转换的结果类型,其值可以为0, 8, 16, 32和64,分别对应 int, int8, int16, int32和int64

(5)float相关

float转string:

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

函数原型及参数含义具体可查看:https://golang.org/pkg/strconv/#FormatFloat

string转float:

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

 PS:go语言string、int、int64互相转换

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':其它情况)

猜你喜欢

转载自www.cnblogs.com/skzxc/p/11956309.html
今日推荐