golang数字转字符串方法

在golang中,有多种数字转字符串的方法。

1)fmt方法

fmt 包应该是最常见的了,从刚开始学习 Golang 就接触到了,写 ‘hello, world' 就得用它。它还支持格式化变量转为字符串。

关于fmt的sprintf的定义如下:

func Sprintf(format string, a ...interface{}) string
Sprintf formats according to a format specifier and returns the resulting string.
fmt.Sprintf("%d", a)

2)strconv.Itoa

strconc.Itoa是封装了strconv.FormatInt,定义如下:

func Itoa(i int) string
Itoa is shorthand for FormatInt(int64(i), 10).
strconv.Itoa(a)

3)strconv.FormatInt

strconv.FormatInt定义如下:
func FormatInt(i int64, base int) string
FormatInt returns the string representation of i in the given base, for 2 <= b

 

这几种方法里面:fmt.Sprintf效率是最低的,strconv.Itoa和strconv.FormatInt效率更改;当然,由于strconv.Itoa封装了strconv.FormatInt,效率会小于strconv.FormatInt.

建议对数字进行字符串转转化的时候,尽量用strconv.Format

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值