The documentation for fmt.Printf's formatting fields for floating-point values states that:
"For floating-point values, width sets the minimum width of the field
and precision sets the number of places after the decimal, if
appropriate, except that for %g/%G it sets the total number of digits."
The confusing part lies in the usage of "it" in the last sentence. It actually refers to "precision," not "width."
Contrary to the intuition that %.9g should produce a total of 9 significant digits, it actually only considers digits after the decimal point.
For example, 123456.789 will be formatted as 123.4e 03 because %.9g effectively truncates any leading zeros.
Conversely, 0.0000012345 will be formatted as 1.2345e-06 because %.9g considers all significant digits after the decimal point.
The width field specifies the minimum width of the entire formatted string, including the decimal point and exponent (if any).
The precision field, as described above, sets the number of significant digits after the decimal point.
For %g, the precision field overrides the width field. However, if the formatted string exceeds the specified width, it will extend beyond the specified width.
Recall that the example involves formatting the following floating-point values using .9g:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3