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)

6.整形到float或者float到整形

直接使用float(i) 或者 int(f) 直接进行转换即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值