]> Cypherpunks repositories - gostls13.git/commitdiff
big: Rat always outputs the requested precision from FloatString
authorGraham Miller <graham.miller@gmail.com>
Mon, 6 Jun 2011 19:59:58 +0000 (12:59 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 6 Jun 2011 19:59:58 +0000 (12:59 -0700)
Fixes #1922.

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/4551098

src/pkg/big/rat.go
src/pkg/big/rat_test.go

index f11c27425ce8704b21902e9d3becd4d5c49d9ed6..b2e93f2a450c29ab6a52a5868c97ba4b08551432 100644 (file)
@@ -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)
index ae5c7c9936384bc7ca29ca0a8f98aa82aa5092fd..4effbf8eac3f683493c0c0a4c7cde6495dc9b8dd 100644 (file)
@@ -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"},