`%g`在`Printf`中不能像`%f`或`%e`那样给出固定的小数位数。

huangapple go评论195阅读模式
英文:

%g in Printf does not give fixed decimal places like %f or %e

问题

根据这些文档%g在大指数时等同于%e,在其他情况下等同于%f。然而,当我运行上述代码时,%g的输出结果为2,而不像%e%f那样包含固定的小数位。

为什么%g的输出结果不像%e%f那样包含固定的小数位呢?

英文:

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\n", a)
	fmt.Printf("%e\n", a)
	fmt.Printf("%g\n", 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

得分: 2

包 fmt

打印

%g 的默认精度是最小的数字,足以唯一标识该值。

对于值 2.0,这是一个数字,给出值 2。

英文:

> 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.

huangapple
  • 本文由 发表于 2015年10月16日 06:55:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/33159873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定