fmt.Sprintf rounds float numbers

blov · · 693 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Is this supposed to work like that ? I was expecting to have 0.010.</p> <pre><code>a := 0.010510757102972574 b := fmt.Sprintf(&#34;%.3f&#34;, 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

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