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
?
根据对于大指数,这些文档 我得到: p>
%g code>是
%e,否则为%f code>。 但是,当我这样做时: p>
包main
import“ fmt”
func main(){
var a float64 = 2.0
fmt.Printf( “%f
”,a)
fmt.Printf(“%e
”,a)
fmt.Printf(“%g
”,a)
}
code> pre>
2.000000
2.000000e + 00
2
code> pre>
为什么
%g code>的输出不包含固定的小数位,例如
%e code>或
%f code>? p>
DIV>
答
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.