<p>Is this supposed to work like that ? I was expecting to have 0.010.</p>
<pre><code>a := 0.010510757102972574
b := fmt.Sprintf("%.3f", a)
fmt.Println(b)
// 0.011
</code></pre>
<p><a href="https://play.golang.org/p/BYpbVQeRyC" rel="nofollow">https://play.golang.org/p/BYpbVQeRyC</a></p>
<hr/>**评论:**<br/><br/>lstokeworth: <pre><p>Yes. The <a href="https://godoc.org/strconv#FormatFloat" rel="nofollow">documentation</a> for the underlying format function says:</p>
<blockquote>
<p>FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).</p>
</blockquote></pre>prvst: <pre><p>thanks</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传