aboutsummaryrefslogtreecommitdiff
path: root/src/fmt
diff options
context:
space:
mode:
authorFilippo Valsorda <filippo@golang.org>2018-10-16 13:01:07 -0400
committerFilippo Valsorda <filippo@golang.org>2018-10-16 21:54:35 +0000
commita52289ef2bf7e9b7a619caecc14d6c95eff9eeae (patch)
tree5baa3e49cc86a1e4bbebefe1e344d9d442d8025b /src/fmt
parent965fa3b191270bbc23a040a520ce43406ba29343 (diff)
downloadgo-a52289ef2bf7e9b7a619caecc14d6c95eff9eeae.tar.gz
go-a52289ef2bf7e9b7a619caecc14d6c95eff9eeae.zip
Revert "fmt: fix incorrect format of whole-number floats when using %#v"
Numbers without decimals are valid Go representations of whole-number floats. That is, "var x float64 = 5" is valid Go. Avoid breakage in tests that expect a certain output from %#v by reverting to it. To guarantee the right type is generated by a print use %T(%#v) instead. Added a test to lock in this behavior. This reverts commit 7c7cecc1846aaaa0ce73931644fe1df2b4559e09. Fixes #27634 Updates #26363 Change-Id: I544c400a0903777dd216452a7e86dfe60b0b0283 Reviewed-on: https://go-review.googlesource.com/c/142597 Run-TryBot: Filippo Valsorda <filippo@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/fmt')
-rw-r--r--src/fmt/fmt_test.go7
-rw-r--r--src/fmt/format.go41
2 files changed, 18 insertions, 30 deletions
diff --git a/src/fmt/fmt_test.go b/src/fmt/fmt_test.go
index d63271a805..e97372225c 100644
--- a/src/fmt/fmt_test.go
+++ b/src/fmt/fmt_test.go
@@ -686,11 +686,10 @@ var fmtTests = []struct {
{"%#v", 1.2345678, "1.2345678"},
{"%#v", float32(1.2345678), "1.2345678"},
- // Whole number floats should have a single trailing zero added, but not
- // for exponent notation.
- {"%#v", 1.0, "1.0"},
+ // Whole number floats are printed without decimals. See Issue 27634.
+ {"%#v", 1.0, "1"},
{"%#v", 1000000.0, "1e+06"},
- {"%#v", float32(1.0), "1.0"},
+ {"%#v", float32(1.0), "1"},
{"%#v", float32(1000000.0), "1e+06"},
// Only print []byte and []uint8 as type []byte if they appear at the top level.
diff --git a/src/fmt/format.go b/src/fmt/format.go
index 3a3cd8d1a1..91103f2c07 100644
--- a/src/fmt/format.go
+++ b/src/fmt/format.go
@@ -481,19 +481,15 @@ func (f *fmt) fmtFloat(v float64, size int, verb rune, prec int) {
return
}
// The sharp flag forces printing a decimal point for non-binary formats
- // and retains trailing zeros, which we may need to restore. For the sharpV
- // flag, we ensure a single trailing zero is present if the output is not
- // in exponent notation.
- if f.sharpV || (f.sharp && verb != 'b') {
+ // and retains trailing zeros, which we may need to restore.
+ if f.sharp && verb != 'b' {
digits := 0
- if !f.sharpV {
- switch verb {
- case 'g', 'G':
- digits = prec
- // If no precision is set explicitly use a precision of 6.
- if digits == -1 {
- digits = 6
- }
+ switch verb {
+ case 'v', 'g', 'G':
+ digits = prec
+ // If no precision is set explicitly use a precision of 6.
+ if digits == -1 {
+ digits = 6
}
}
@@ -502,32 +498,25 @@ func (f *fmt) fmtFloat(v float64, size int, verb rune, prec int) {
var tailBuf [5]byte
tail := tailBuf[:0]
- var hasDecimalPoint, hasExponent bool
+ hasDecimalPoint := false
// Starting from i = 1 to skip sign at num[0].
for i := 1; i < len(num); i++ {
switch num[i] {
case '.':
hasDecimalPoint = true
case 'e', 'E':
- hasExponent = true
tail = append(tail, num[i:]...)
num = num[:i]
default:
digits--
}
}
- if f.sharpV {
- if !hasDecimalPoint && !hasExponent {
- num = append(num, '.', '0')
- }
- } else {
- if !hasDecimalPoint {
- num = append(num, '.')
- }
- for digits > 0 {
- num = append(num, '0')
- digits--
- }
+ if !hasDecimalPoint {
+ num = append(num, '.')
+ }
+ for digits > 0 {
+ num = append(num, '0')
+ digits--
}
num = append(num, tail...)
}