[英]How to convert uint64 to string
我正在嘗試使用uint64
打印string
,但我使用的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
?
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: %d\n", charge.Amount)
如果要將int64
轉換為string
,可以使用:
strconv.FormatInt(time.Now().Unix(), 10)
或者
strconv.FormatUint
如果你真的想把它保存在一個字符串中,你可以使用 Sprint 函數之一。 例如:
myString := fmt.Sprintf("%v", charge.Amount)
log.Printf("The amount is: %d\n", charge.Amount)
func main() {
var a uint64
a = 3
var s string
s = fmt.Sprint(a)
fmt.Printf("%s", s)
}
如果您來這里是為了了解如何將字符串轉換為 uint64,這就是它的完成方式:
newNumber, err := strconv.ParseUint("100", 10, 64)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.