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
Assignees
Milestone

Comments

@akiross
Copy link

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

@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

@gopherbot gopherbot commented Apr 13, 2016

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

@gopherbot gopherbot closed this in 933d521 Apr 14, 2016
@golang golang locked and limited conversation to collaborators Apr 14, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.