<p>I'm doing some operations with numbers bigger than 64 bits and it seems that <a href="https://golang.org/pkg/math/big/#Int.SetString" rel="nofollow">SetString</a> does a good job of initializing the numbers.</p>
<p>However I got a problem in getting the number back. It's fine if it's string, but trying to transform the number back to string gives me an output similar to</p>
<pre><code>fmt.Printf("%d\n", bigNumber.String())
> %!d(string=5e+18)
fmt.Printf("%s\n", bigNumber.String())
> 5e+18
fmt.Printf("%v\n", bigNumber.String())
> 5e+18
</code></pre>
<p>How can I get the string representation of a number bigger than 64 bits?</p>
<p>Solution:
As long as the operations are in <code>big.Int</code> realm, the result isn't truncated.</p>
<p>Proof from <a href="https://www.reddit.com/user/deadycool" rel="nofollow">deadycool</a>
<a href="https://play.golang.org/p/Bwge63S3HO" rel="nofollow">https://play.golang.org/p/Bwge63S3HO</a></p>
<hr/>**评论:**<br/><br/>deadycool: <pre><p>It seems to be working fine in golang playground: <a href="https://play.golang.org/p/Bwge63S3HO" rel="nofollow">https://play.golang.org/p/Bwge63S3HO</a></p></pre>hydrarulz: <pre><p>Yes, thanks! ;)</p></pre>kemitche: <pre><p>Did you try <a href="https://golang.org/pkg/math/big/#Int.Text" rel="nofollow">Text()</a>?</p>
<p>Edit: never mind, String() is just Text(10). I'm not able to reproduce. What number are you getting that result for? Here's what I see: <a href="https://play.golang.org/p/Odbj4ZxoSP" rel="nofollow">https://play.golang.org/p/Odbj4ZxoSP</a></p></pre>hydrarulz: <pre><p>Text() has the same problems as Format it only outputs stuff that look like this</p>
<pre><code>'e' -d.dddde±dd, decimal exponent, at least two (possibly 0) exponent digits
'E' -d.ddddE±dd, decimal exponent, at least two (possibly 0) exponent digits
'f' -ddddd.dddd, no exponent
'g' like 'e' for large exponents, like 'f' otherwise
'G' like 'E' for large exponents, like 'f' otherwise
'b' -ddddddp±dd, binary exponent
'p' -0x.dddp±dd, binary exponent, hexadecimal mantissa
</code></pre>
<p>So the inevitable <code>5e+18</code> will still be there.</p>
<p>Possible solution:
It seems that If all my operations are in the <code>big.Int</code> realm, when I transform back to <code>%s</code> the numbers aren't "truncated"</p></pre>kemitche: <pre><p>Oh, you're original post implies you're using big.Int, but that docstring is from <em>big.Float</em>. I would assume that a big.Float representation is almost certainly always going to be truncated in some way, since Floats can't be represented precisely (e.g. they often result in infinitely repeating decimals.</p></pre>hydrarulz: <pre><p>Yup, that was the issue</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传