donglu9825
donglu9825
2015-10-15 22:55

Printf中的%g没有给出固定的小数位数,例如%f或%e

已采纳

According to these docs %g is %e for large exponents, %f otherwise. However, when I do:

package main

import "fmt"

func main() {
    var a float64 = 2.0
    fmt.Printf("%f
", a)
    fmt.Printf("%e
", a)
    fmt.Printf("%g
", a)
}

I get:

2.000000
2.000000e+00
2

Why does the output for %g not contain fixed decimal places like %e or %f?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duanjian3920 duanjian3920 6年前

    Package fmt

    Printing

    The default precision for %g is the smallest number of digits necessary to identify the value uniquely

    For the value 2.0 that is one digit giving the value 2.

    点赞 评论 复制链接分享

相关推荐