Does go have a way to make long numbers more readable?

agolangf · · 438 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Like if I have a number like 44000000000 it&#39;s kind of hard to tell what it is. Some languages allow underscores in numbers like 44_000_000_000. Does go have a way to make large numbers like this more readable?</p> <hr/>**评论:**<br/><br/>Sythe2o0: <pre><p>You can write it in exponential format if that&#39;s more readable for you: <a href="https://play.golang.org/p/Of9j4fwqkG" rel="nofollow">https://play.golang.org/p/Of9j4fwqkG</a></p></pre>natefinch: <pre><p>^ This makes it pretty easy... so like, the number OP mentioned is 44 billion, you can write it as 44e9 And 44 million is 44e6.</p> <p>You can also write it as 44 * 1000 * 1000 * 1000, which can be computed just a single time at runtime if it&#39;s marked as a constant (note that you can declare constants inside a function:</p> <p><a href="https://play.golang.org/p/eHUDHy0Nro" rel="nofollow">https://play.golang.org/p/eHUDHy0Nro</a></p></pre>andreasblixt: <pre><p>The value is actually computed at compile time, not runtime. This means you don&#39;t need to consider the complexity of the math involved – so long as it&#39;s a constant expression it&#39;ll be precomputed into the binary when it&#39;s compiled, just as if you had written the value yourself wherever the constant is used.</p> <p>Another interesting points is that Go constants have arbitrary integer precision so you can technically do this :)</p> <pre><code>const x = 1e100 fmt.Println(x) // prints 1e+100 </code></pre> <p>Note that you can&#39;t assign such a value to any of the number types:</p> <pre><code>var i int64 = x // error: constant 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 overflows int64 </code></pre> <p>But you can combine constants that result in a valid number:</p> <pre><code>const y = 1e95 var i int64 = x / y // i is now 100000 </code></pre> <p>Have a look at the <code>time</code> package source code which uses integer constants nicely: <a href="https://golang.org/src/time/time.go?s=21095:21114#L610">https://golang.org/src/time/time.go?s=21095:21114#L610</a></p></pre>natefinch: <pre><p>Sorry yes, compile time. I thought one thing and typed the other :)</p></pre>: <pre><p>[removed]</p></pre>natefinch: <pre><p>We all make mistakes, even people who have been doing go for a long time. :)</p></pre>jeremiahs_bullfrog: <pre><p>You can also use decimal places as well, even for integers, but it&#39;ll coerce to a <code>float64</code> if you don&#39;t specify the type: <a href="https://play.golang.org/p/YVT54XSXo3" rel="nofollow">https://play.golang.org/p/YVT54XSXo3</a></p></pre>fazalmajid: <pre><p>Have a look at <a href="https://github.com/dustin/go-humanize" rel="nofollow">https://github.com/dustin/go-humanize</a></p></pre>titpetric: <pre><p>At least the ftoa example is redundant:</p> <pre><code>fmt.Printf(&#34;%f&#34;, 2.24) // 2.240000 fmt.Printf(&#34;%s&#34;, humanize.Ftoa(2.24)) // 2.24 </code></pre> <p>S/Printf already supports fixed decimal points formatting with <code>%.Xf</code>, here&#39;s a <a href="https://play.golang.org/p/oTkDZOubwM" rel="nofollow">playground example</a>. But that&#39;s not what the OP asked, so get an upvote. It doesn&#39;t however help him to read numbers from his code, only ones that he prints out :)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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