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: %#g behavior not consistent #36562

Closed
musiphil opened this issue Jan 15, 2020 · 3 comments
Closed

fmt: %#g behavior not consistent #36562

musiphil opened this issue Jan 15, 2020 · 3 comments
Milestone

Comments

@musiphil
Copy link

@musiphil musiphil commented Jan 15, 2020

What version of Go are you using (go version)?

$ go version
go version go1.13.4 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="$HOME/.cache/go-build"
GOENV="$HOME/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="$HOME/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/google-golang"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/google-golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build990462557=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/uhRa_BD_pUX

fmt.Printf("%#.4g\n", 1.234)
fmt.Printf("%#.4g\n", 0.1234)
fmt.Printf("%#.4g\n", 1.23)
fmt.Printf("%#.4g\n", 0.123)
fmt.Printf("%#.4g\n", 1.2)
fmt.Printf("%#.4g\n", 0.12)

What did you expect to see?

1.234
0.1234
1.230
0.1230
1.200
0.1200

%#.4g should print 4 significant digits, without removing trailing zeros.

What did you see instead?

1.234
0.1234
1.230
0.123
1.200
0.120

The trailing zero from 1.230 is erroneously removed.

@martisch

This comment has been minimized.

Copy link
Member

@martisch martisch commented Jan 15, 2020

This seems consistent with the documentation (https://godoc.org/fmt) of the g verb and precision:

"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 precision sets the maximum number of significant digits (trailing zeros are removed)."

@martisch

This comment has been minimized.

Copy link
Member

@martisch martisch commented Jan 15, 2020

I see now that # is supposed to alter the g verb behavior to not remove trailing zeros later in the documentation.

"do not remove trailing zeros for %g and %G;"

So you are right this should have trailing zeros.

@martisch martisch added this to the Go1.15 milestone Jan 15, 2020
@martisch martisch self-assigned this Jan 15, 2020
yah01 added a commit to yah01/go that referenced this issue Jan 15, 2020
@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Jan 15, 2020

Change https://golang.org/cl/215001 mentions this issue: fmt: do not remove trailing zeros for %g and %G with #(sharp) flag

yah01 added a commit to yah01/go that referenced this issue Jan 16, 2020
yah01 added a commit to yah01/go that referenced this issue Jan 18, 2020
yah01 added a commit to yah01/go that referenced this issue Jan 18, 2020
@martisch martisch removed their assignment Jan 18, 2020
@gopherbot gopherbot closed this in 42b93b7 Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.