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

(1)int转string

s := strconv.Itoa(i)

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

(2)int64转string

i := int64(123)

s := strconv.FormatInt(i, 10)

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

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

(3)string转int

i, err := strconv.Atoi(s)

(4)string转int64

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

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

(5)float相关

float转string:

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:

s := "3.1415926535"

v1, err := strconv.ParseFloat(v, 32)

v2, err := strconv.ParseFloat(v, 64)

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

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

总结

以上所述是小编给大家介绍的Go语言string,int,int64 ,float之间类型转换方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

本文标题: Go语言string,int,int64 ,float之间类型转换方法

本文地址: http://www.cppcns.com/jiaoben/golang/197258.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值