加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在golang中将uint64转换为字符串

发布时间:2020-12-16 19:19:48 所属栏目:大数据 来源:网络整理
导读:我试图用uint64打印一个字符串,但我没有使用strconv方法的组合。 log.Println("The amount is: " + strconv.Itoa((charge.Amount))) 给我: 不能使用charge.Amount(类型uint64)作为strconv.Itoa参数中的int类型 我该如何打印这个字符串? strconv.Itoa() 期
我试图用uint64打印一个字符串,但我没有使用strconv方法的组合。
log.Println("The amount is: " + strconv.Itoa((charge.Amount)))

给我:

不能使用charge.Amount(类型uint64)作为strconv.Itoa参数中的int类型

我该如何打印这个字符串?

strconv.Itoa()期望int类型的值,所以你必须给它:
log.Println("The amount is: " + strconv.Itoa(int(charge.Amount)))

但是要知道如果int是32位(而uint64是64),这可能会失去精度,而且签名也是不同的。 strconv.FormatUint()会更好,因为它需要uint64类型的值:

log.Println("The amount is: " + strconv.FormatUint(charge.Amount,10))

有关更多选项,请参阅以下答案:Golang: format a string without printing?

如果您的目的是仅打印该值,则无需将其转换为int或string,请使用以下方法之一:

log.Println("The amount is:",charge.Amount)
log.Printf("The amount is: %dn",charge.Amount)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读