源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-05-25 22:15 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Go语言string,int,int64 ,float之间类型转换方法
[b](1)int转string[/b]
s := strconv.Itoa(i)
等价于s := strconv.FormatInt(int64(i), 10)
[b](2)int64转string[/b]
i := int64(123)
s := strconv.FormatInt(i, 10)
第二个参数为基数,可选2~36 注:对于无符号整形,可以使用[code]FormatUint(i uint64, base int)[/code] [b](3)string转int[/b]
i, err := strconv.Atoi(s)
[b](4)string转int64[/b]
i, err := strconv.ParseInt(s, 10, 64)
第二个参数为基数(2~36),第三个参数位大小表示期望转换的结果类型,其值可以为0, 8, 16, 32和64,分别对应 int, int8, int16, int32和int64 [b](5)float相关[/b] float转string:
v := 3.1415926535
s1 := strconv.FormatFloat(v, 'E', -1, 32)//float32s2 := strconv.FormatFloat(v, 'E', -1, 64)//float64
函数原型及参数含义具体可查看:[url=https://golang.org/pkg/strconv/#FormatFloat]https://golang.org/pkg/strconv/#FormatFloat[/url] string转float:
s := "3.1415926535"
v1, err := strconv.ParseFloat(v, 32)
v2, err := strconv.ParseFloat(v, 64)
[b] PS:go语言string、int、int64互相转换[/b]
//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':其它情况)
[b]总结[/b] 以上所述是小编给大家介绍的Go语言string,int,int64 ,float之间类型转换方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程素材网网站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部