diff options
author | Graham Miller <graham.miller@gmail.com> | 2011-06-06 12:59:58 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2011-06-06 12:59:58 -0700 |
commit | f35a3df80c9df2c5bf2651746b2df047d356c8a4 (patch) | |
tree | 2c0033e43751a0cbd025e55cccf51204d8d54ad6 | |
parent | c281ddf1eb4e6cff20096322f73724d2b21b74de (diff) | |
download | go-f35a3df80c9df2c5bf2651746b2df047d356c8a4.tar.gz go-f35a3df80c9df2c5bf2651746b2df047d356c8a4.zip |
big: Rat always outputs the requested precision from FloatString
Fixes #1922.
R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/4551098
-rw-r--r-- | src/pkg/big/rat.go | 6 | ||||
-rw-r--r-- | src/pkg/big/rat_test.go | 5 |
2 files changed, 8 insertions, 3 deletions
diff --git a/src/pkg/big/rat.go b/src/pkg/big/rat.go index f11c27425c..b2e93f2a45 100644 --- a/src/pkg/big/rat.go +++ b/src/pkg/big/rat.go @@ -314,7 +314,11 @@ func (z *Rat) RatString() string { // digits of precision after the decimal point and the last digit rounded. func (z *Rat) FloatString(prec int) string { if z.IsInt() { - return z.a.String() + s := z.a.String() + if prec > 0 { + s += "." + strings.Repeat("0", prec) + } + return s } q, r := nat{}.div(nat{}, z.a.abs, z.b) diff --git a/src/pkg/big/rat_test.go b/src/pkg/big/rat_test.go index ae5c7c9936..4effbf8eac 100644 --- a/src/pkg/big/rat_test.go +++ b/src/pkg/big/rat_test.go @@ -86,12 +86,13 @@ var floatStringTests = []struct { out string }{ {"0", 0, "0"}, - {"0", 4, "0"}, + {"0", 4, "0.0000"}, {"1", 0, "1"}, - {"1", 2, "1"}, + {"1", 2, "1.00"}, {"-1", 0, "-1"}, {".25", 2, "0.25"}, {".25", 1, "0.3"}, + {".25", 3, "0.250"}, {"-1/3", 3, "-0.333"}, {"-2/3", 4, "-0.6667"}, {"0.96", 1, "1.0"}, |