Transform number bigger than 64 bits to a string

agolangf · · 303 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;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&#39;s fine if it&#39;s string, but trying to transform the number back to string gives me an output similar to</p> <pre><code>fmt.Printf(&#34;%d\n&#34;, bigNumber.String()) &gt; %!d(string=5e+18) fmt.Printf(&#34;%s\n&#34;, bigNumber.String()) &gt; 5e+18 fmt.Printf(&#34;%v\n&#34;, bigNumber.String()) &gt; 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&#39;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&#39;m not able to reproduce. What number are you getting that result for? Here&#39;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>&#39;e&#39; -d.dddde±dd, decimal exponent, at least two (possibly 0) exponent digits &#39;E&#39; -d.ddddE±dd, decimal exponent, at least two (possibly 0) exponent digits &#39;f&#39; -ddddd.dddd, no exponent &#39;g&#39; like &#39;e&#39; for large exponents, like &#39;f&#39; otherwise &#39;G&#39; like &#39;E&#39; for large exponents, like &#39;f&#39; otherwise &#39;b&#39; -ddddddp±dd, binary exponent &#39;p&#39; -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&#39;t &#34;truncated&#34;</p></pre>kemitche: <pre><p>Oh, you&#39;re original post implies you&#39;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&#39;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

303 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传