-
Notifications
You must be signed in to change notification settings - Fork 18k
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: sub-10
exponents in formatted floats are zero-padded to at least two digits
#70862
Comments
It's because that's what C does for the %e and %E formats:
I suspect the behavior in Go for %b is more an accident then anything deliberate but probably won't change. |
FWIW, the C behavior (which does seem bizarre) is documented in the standard describing
|
I was holding out some hope this could be fixed since it's previously been stated that
I sort of have to imagine that this traces its lineage to a float formatter that didn't consider doubles? But ostensibly that would have been pre-C. |
No, it's not beholden but it's also designed to be like it, especially when dealing with the same things. And because of the compatibility promise, the behavior is very unlikely to change. It's just too minor a thing to break compatibility with the past (and with C, to be honest). |
Change https://go.dev/cl/636915 mentions this issue: |
Thank you @ianlancetaylor! |
Except for %b where it is only one. Fixes golang#70862 Change-Id: Ic423a799b73bfa534f4083f6544bb9cd639fef06 Reviewed-on: https://go-review.googlesource.com/c/go/+/636915 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
Go version
go version go1.23.4 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
Using
%e
,%E
,%x
, or%X
to format a floating-point value that ends up with an exponent between0
and9
.https://go.dev/play/p/oaSxLmPizik
What did you see happen?
The exponent value is zero-padded to two digits. Interestingly,
%b
does not exhibit this behavior.What did you expect to see?
The exponent value is not zero-padded.
I realize it may be a bit cheeky to file this as a bug, given that the behavior seems deliberately coded. But, I couldn't find any documentation saying that it is correct behavior, and it doesn't seem to make much sense given that it does not result in a uniform length for exponents since they can also be three digits long.
I am also genuinely curious if it is intentional that
%b
behaves differently in this respect.The text was updated successfully, but these errors were encountered: