英文:
Convert a bigint to a string in Go
问题
如何将一个大整数转换为字符串(或整数)在Golang中?
bigint := big.NewInt(123) //这是我有的
bigstr = "123" //这是我想要的
英文:
How do one convert a big int to a string (or integer) in Golang?
bigint := big.NewInt(123) //This is what I have
bigstr = "123" //This is what I want
答案1
得分: 36
只需使用String
方法:http://golang.org/pkg/math/big/#Int.String
bigint := big.NewInt(123)
bigstr := bigint.String()
英文:
Just use the String
method : http://golang.org/pkg/math/big/#Int.String
bigint := big.NewInt(123)
bigstr := bigint.String()
答案2
得分: 4
你问如何将bigInt转换为字符串或整数,被接受的答案只解释了如何转换为字符串。
所以你有你的bigint := big.NewInt(123)
你可以用两种方式将bigInt转换为整数:
之所以有两种方法是因为uint可以容纳两倍大的数字,有时你知道答案是正数。请注意,如果数字大于int64/uint64的最大可能值:
> 如果x无法表示为int64,则结果是未定义的。
为了完整起见,要转换为字符串,只需使用.String() bigstr := bigint.String()
英文:
You asked how to convert a bigInt to string or to int, the accepted answer explains only how to convert to string.
So you have your bigint := big.NewInt(123)
You can convert your bigInt to integer in two possible ways:
The reason for two methods is that uint holds 2 time bigger numbers, and sometimes you know that the answer is positive. Beware that if the number is bigger than the maximum possible for int64/uint64:
> If x cannot be represented in an int64, the result is undefined.
And for completeness, to convert to string, just use .String() bigstr := bigint.String()
答案3
得分: 0
bigint := big.NewInt(1231231231231)
bigstr := fmt.Sprint(bigint)
英文:
I used the following:
bigint := big.NewInt(1231231231231)
bigstr := fmt.Sprint(bigint)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论