如何将uint64转换为字符串?

huangapple go评论83阅读模式
英文:

How to convert uint64 to string

问题

我正在尝试打印一个包含uint64string,但是我使用的所有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

英文:

I am trying to print a string with a uint64 but no combination of strconv methods that I use is working.

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

Gives me:

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

How can I print this string?

答案1

得分: 117

strconv.Itoa()函数期望一个int类型的值作为参数,所以你需要给它一个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))

如果你只是想打印这个值,你不需要转换成int或者string,可以使用下面这些方法之一:

log.Println("The amount is:", charge.Amount)
log.Printf("The amount is: %d\n", charge.Amount)
英文:

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: https://stackoverflow.com/questions/11123865/golang-format-a-string-without-printing/31742265#31742265

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\n", charge.Amount)

答案2

得分: 52

如果你想将int64转换为string,你可以使用:

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

或者

strconv.FormatUint
英文:

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

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

or

strconv.FormatUint

答案3

得分: 11

如果你真的想将它保留在一个字符串中,你可以使用Sprint函数之一。例如:

myString := fmt.Sprintf("%v", charge.Amount)
英文:

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)

答案4

得分: 0

func main() {
    var a uint64
    a = 3
    var s string
    s = fmt.Sprint(a)
    fmt.Printf("%s", s)
}
func main() {
    var a uint64
    a = 3
    var s string
    s = fmt.Sprint(a)
    fmt.Printf("%s", s)
}
func main() {
    var a uint64
    a = 3
    var s string
    s = fmt.Sprint(a)
    fmt.Printf("%s", s)
}
英文:
func main() {
	var a uint64
	a = 3
	var s string
	s = fmt.Sprint(a)
	fmt.Printf("%s", s)
}

答案5

得分: -1

log.Printf

log.Printf("金额为:%d\n", charge.Amount)
英文:

log.Printf

log.Printf("The amount is: %d\n", charge.Amount)

答案6

得分: -4

如果你来到这里是为了了解如何将字符串转换为uint64,下面是实现的方法:

newNumber, err := strconv.ParseUint("100", 10, 64)

这段代码可以将字符串"100"转换为uint64类型的数字。

英文:

If you came here looking on how to covert string to uint64, this is how its done:
<!-- language: lang-go -->

newNumber, err := strconv.ParseUint(&quot;100&quot;, 10, 64)

huangapple
  • 本文由 发表于 2017年1月22日 13:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/41787620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定