Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: documentation unclear regarding %g precision formatting #15178

Closed
akiross opened this issue Apr 7, 2016 · 2 comments
Closed

fmt: documentation unclear regarding %g precision formatting #15178

akiross opened this issue Apr 7, 2016 · 2 comments

Comments

@akiross
Copy link

akiross commented Apr 7, 2016

Current go 1.5 documentation is not clear regarding the %g formatting. In particular, it states

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. For example, given 123.45 the format %6.2f prints 123.45 while %.4g prints 123.5. The default precision for %e and %f is 6; for %g it is the smallest number of digits necessary to identify the value uniquely.

This seems to imply that width for %g/%G is the total number of digits that will be outputted, while it is the number of significant digits, therefore leading zeroes does not contribute to the count. For example, formatting 0.001234 with %.3g will be printed without changes, 0.001234, while from the docs it seems that only 0.001 should be printed.

I propose to change the documentation and change the following:

except that for %g/%G it sets the total number of digits. For example, given 123.45 the format %6.2f prints 123.45 while %.4g prints 123.5.

with the following (bold used to mark the changes)

except that for %g/%G it sets the total number of significant digits. For example, given 123.45 the format %6.2f prints 123.45 while %.4g prints 123.5**, and 0.00123 is printed as 0.001 by %.3f, but printed as 0.00123 by %.3g**.

Or with a better statement and/or example if the one I provided are not accurate enough.

@akiross
Copy link
Author

akiross commented Apr 7, 2016

@bradfitz bradfitz changed the title Documentation unclear regarding %g precision formatting fmt: documentation unclear regarding %g precision formatting Apr 7, 2016
@bradfitz bradfitz added this to the Go1.7 milestone Apr 7, 2016
@gopherbot
Copy link

CL https://golang.org/cl/22006 mentions this issue.

@golang golang locked and limited conversation to collaborators Apr 14, 2017
@rsc rsc unassigned robpike Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants