dousong5492
2017-01-22 05:18
采纳率: 100%
浏览 2.6k
已采纳

在Golang中将uint64转换为字符串

我正试图打印 stringuint64 但是我使用的strconv方法的组合是无效的。

log.Println("The amount is: " + strconv.Itoa((charge.Amount)))

给我的提示是:

cannot use charge.Amount (type uint64) as type int in argument to strconv.Itoa

那么我该如何打印 string这个呢?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用 uint64 打印 string 但我使用的 strconv 方法没有任何组合有效。

  log.Println(“数量为:” + strconv.Itoa((charge  .Amount))))
   
 
 

给我:

不能使用charge.Amount(类型uint64)作为类型 int in strconv.Itoa

如何打印此 string

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dongwu1992 2017-01-22 05:23
    已采纳

    strconv.Itoa() expects a value of type int, so you have to give it that:

    log.Println("The amount is: " + strconv.Itoa(int(charge.Amount)))
    

    But know that this may lose precision if int is 32-bit (while uint64 is 64), also sign-ness is different. strconv.FormatUint() would be better as that expects a value of type uint64:

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

    For more options, see this answer: Golang: format a string without printing?

    If your purpose is to just print the value, you don't need to convert it, neither to int nor to string, use one of these:

    log.Println("The amount is:", charge.Amount)
    log.Printf("The amount is: %d
    ", charge.Amount)
    
    点赞 打赏 评论
  • douwenpin0428 2017-01-22 05:23

    log.Printf

    log.Printf("The amount is: %d
    ", charge.Amount)
    
    点赞 打赏 评论
  • dtkago3208 2017-01-23 11:09

    If you actually want to keep it in a string you can use one of Sprint functions. For instance:

    myString := fmt.Sprintf("%v", charge.Amount)
    
    点赞 打赏 评论
  • douyue7536 2017-11-08 13:27

    if you want to convert int64 to string, you can use :

    strconv.FormatInt(time.Now().Unix(), 10)
    

    or

    strconv.FormatUint
    
    点赞 打赏 评论
  • dongwen1909 2019-02-25 14:09

    If you came here looking on how to covert string to uint64, this is how its done:

    newNumber, err := strconv.ParseUint("100", 10, 64)
    
    点赞 打赏 评论

相关推荐 更多相似问题