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: unexpected trailing period with alternate floating point formats #37744

Closed
mpx opened this issue Mar 8, 2020 · 2 comments
Closed

fmt: unexpected trailing period with alternate floating point formats #37744

mpx opened this issue Mar 8, 2020 · 2 comments

Comments

@mpx
Copy link
Contributor

mpx commented Mar 8, 2020

go version go1.14 linux/amd64

Using the alternate floating point formats adds an unexpected trailing period when the fractional part falls just outside the significant figures (playground link):

fmt.Printf("%#.0f\n", 123.0)
fmt.Printf("%#.3g\n", 123.0)

What did you expect to see?

123
123

What did you see instead?

123.
123.

This is awkward when displaying values for consumption by people. Eg:

fmt.Printf("%#.3gm", 123.0) gives 123.m instead of the expected 123m.

The alternate format for %f and %g was added in e97f407.

C printf behaves as expected and does not print trailing periods.

Cc @robpike @martisch

@robpike
Copy link
Contributor

robpike commented Mar 8, 2020

As the document added in that commit says, the definition of this format is to always include the decimal point. That's what it's for. Compare with https://play.golang.org/p/lS5Z2QkCDFp, which shows how to get what you want: don't ask for the alternate format.

Moreover, in this respect I believe the behavior is completely compatible with C, which also adds a decimal point in the alternate format.

% cat x.c
#include <stdio.h>

int
main(int argc, char *argv[]) {
  printf("%#.0f\n", 123.0);
}
% gcc x.c
% a.out
123.
% 

Working as intended.

@robpike robpike closed this as completed Mar 8, 2020
@mpx
Copy link
Contributor Author

mpx commented Mar 8, 2020

Oops, you're right - I mangled my testing in C. I was after the trailing zeros, but the not the trailing decimal point. I'll fix it in post.

@golang golang locked and limited conversation to collaborators Mar 8, 2021
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

3 participants