summaryrefslogtreecommitdiff
path: root/src/pkg/fmt
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-11-20 11:04:51 -0800
committerRob Pike <r@golang.org>2009-11-20 11:04:51 -0800
commit37c59fc0b9d3b46576fcd96a8c1420b09d599a13 (patch)
tree08a6130f29e7965ad880120f24a9c1bb62d12481 /src/pkg/fmt
parent0b471c4c0fdf44a4e354f21fcee1af058f95d31c (diff)
downloadgolang-37c59fc0b9d3b46576fcd96a8c1420b09d599a13.tar.gz
add unimplemented %+ and % (space) flags to floating-point print.
fix %E: was same as %e. add tests. Fixes issue 278. R=rsc CC=golang-dev http://codereview.appspot.com/157111
Diffstat (limited to 'src/pkg/fmt')
-rw-r--r--src/pkg/fmt/fmt_test.go14
-rw-r--r--src/pkg/fmt/format.go34
2 files changed, 37 insertions, 11 deletions
diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go
index 5d9b3abe5..3d9d9bf6f 100644
--- a/src/pkg/fmt/fmt_test.go
+++ b/src/pkg/fmt/fmt_test.go
@@ -101,8 +101,22 @@ var fmttests = []fmtTest{
fmtTest{"%010.3d", -1, " -001"},
fmtTest{"%+d", 12345, "+12345"},
fmtTest{"%+d", -12345, "-12345"},
+ fmtTest{"%+d", 0, "+0"},
+ fmtTest{"% d", 0, " 0"},
fmtTest{"% d", 12345, " 12345"},
+ // floats
+ fmtTest{"%+.3e", 0.0, "+0.000e+00"},
+ fmtTest{"%+.3e", 1.0, "+1.000e+00"},
+ fmtTest{"%+.3f", -1.0, "-1.000"},
+ fmtTest{"% .3E", -1.0, "-1.000E+00"},
+ fmtTest{"% .3e", 1.0, " 1.000e+00"},
+ fmtTest{"%+.3g", 0.0, "+0"},
+ fmtTest{"%+.3g", 1.0, "+1"},
+ fmtTest{"%+.3g", -1.0, "-1"},
+ fmtTest{"% .3g", -1.0, "-1"},
+ fmtTest{"% .3g", 1.0, " 1"},
+
// erroneous formats
fmtTest{"", 2, "?(extra int=2)"},
fmtTest{"%d", "hello", "%d(string=hello)"},
diff --git a/src/pkg/fmt/format.go b/src/pkg/fmt/format.go
index 8407fbe0b..bf13ac314 100644
--- a/src/pkg/fmt/format.go
+++ b/src/pkg/fmt/format.go
@@ -422,33 +422,45 @@ func fmtString(f *Fmt, s string) *Fmt {
return f;
}
+// Add a plus sign or space to the string if missing and required.
+func (f *Fmt) plusSpace(s string) *Fmt {
+ if s[0] != '-' {
+ if f.plus {
+ s = "+" + s
+ } else if f.space {
+ s = " " + s
+ }
+ }
+ return fmtString(f, s);
+}
+
// Fmt_e64 formats a float64 in the form -1.23e+12.
func (f *Fmt) Fmt_e64(v float64) *Fmt {
- return fmtString(f, strconv.Ftoa64(v, 'e', doPrec(f, 6)))
+ return f.plusSpace(strconv.Ftoa64(v, 'e', doPrec(f, 6)))
}
// Fmt_E64 formats a float64 in the form -1.23E+12.
func (f *Fmt) Fmt_E64(v float64) *Fmt {
- return fmtString(f, strconv.Ftoa64(v, 'E', doPrec(f, 6)))
+ return f.plusSpace(strconv.Ftoa64(v, 'E', doPrec(f, 6)))
}
// Fmt_f64 formats a float64 in the form -1.23.
func (f *Fmt) Fmt_f64(v float64) *Fmt {
- return fmtString(f, strconv.Ftoa64(v, 'f', doPrec(f, 6)))
+ return f.plusSpace(strconv.Ftoa64(v, 'f', doPrec(f, 6)))
}
// Fmt_g64 formats a float64 in the 'f' or 'e' form according to size.
func (f *Fmt) Fmt_g64(v float64) *Fmt {
- return fmtString(f, strconv.Ftoa64(v, 'g', doPrec(f, -1)))
+ return f.plusSpace(strconv.Ftoa64(v, 'g', doPrec(f, -1)))
}
// Fmt_g64 formats a float64 in the 'f' or 'E' form according to size.
func (f *Fmt) Fmt_G64(v float64) *Fmt {
- return fmtString(f, strconv.Ftoa64(v, 'G', doPrec(f, -1)))
+ return f.plusSpace(strconv.Ftoa64(v, 'G', doPrec(f, -1)))
}
// Fmt_fb64 formats a float64 in the form -123p3 (exponent is power of 2).
-func (f *Fmt) Fmt_fb64(v float64) *Fmt { return fmtString(f, strconv.Ftoa64(v, 'b', 0)) }
+func (f *Fmt) Fmt_fb64(v float64) *Fmt { return f.plusSpace(strconv.Ftoa64(v, 'b', 0)) }
// float32
// cannot defer to float64 versions
@@ -456,27 +468,27 @@ func (f *Fmt) Fmt_fb64(v float64) *Fmt { return fmtString(f, strconv.Ftoa64(v, '
// Fmt_e32 formats a float32 in the form -1.23e+12.
func (f *Fmt) Fmt_e32(v float32) *Fmt {
- return fmtString(f, strconv.Ftoa32(v, 'e', doPrec(f, 6)))
+ return f.plusSpace(strconv.Ftoa32(v, 'e', doPrec(f, 6)))
}
// Fmt_E32 formats a float32 in the form -1.23E+12.
func (f *Fmt) Fmt_E32(v float32) *Fmt {
- return fmtString(f, strconv.Ftoa32(v, 'e', doPrec(f, 6)))
+ return f.plusSpace(strconv.Ftoa32(v, 'E', doPrec(f, 6)))
}
// Fmt_f32 formats a float32 in the form -1.23.
func (f *Fmt) Fmt_f32(v float32) *Fmt {
- return fmtString(f, strconv.Ftoa32(v, 'f', doPrec(f, 6)))
+ return f.plusSpace(strconv.Ftoa32(v, 'f', doPrec(f, 6)))
}
// Fmt_g32 formats a float32 in the 'f' or 'e' form according to size.
func (f *Fmt) Fmt_g32(v float32) *Fmt {
- return fmtString(f, strconv.Ftoa32(v, 'g', doPrec(f, -1)))
+ return f.plusSpace(strconv.Ftoa32(v, 'g', doPrec(f, -1)))
}
// Fmt_G32 formats a float32 in the 'f' or 'E' form according to size.
func (f *Fmt) Fmt_G32(v float32) *Fmt {
- return fmtString(f, strconv.Ftoa32(v, 'G', doPrec(f, -1)))
+ return f.plusSpace(strconv.Ftoa32(v, 'G', doPrec(f, -1)))
}
// Fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2).