Commit f35a3df8 authored by Graham Miller's avatar Graham Miller Committed by Robert Griesemer

big: Rat always outputs the requested precision from FloatString

Fixes #1922.

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/4551098
parent c281ddf1
...@@ -314,7 +314,11 @@ func (z *Rat) RatString() string { ...@@ -314,7 +314,11 @@ func (z *Rat) RatString() string {
// digits of precision after the decimal point and the last digit rounded. // digits of precision after the decimal point and the last digit rounded.
func (z *Rat) FloatString(prec int) string { func (z *Rat) FloatString(prec int) string {
if z.IsInt() { 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) q, r := nat{}.div(nat{}, z.a.abs, z.b)
......
...@@ -86,12 +86,13 @@ var floatStringTests = []struct { ...@@ -86,12 +86,13 @@ var floatStringTests = []struct {
out string out string
}{ }{
{"0", 0, "0"}, {"0", 0, "0"},
{"0", 4, "0"}, {"0", 4, "0.0000"},
{"1", 0, "1"}, {"1", 0, "1"},
{"1", 2, "1"}, {"1", 2, "1.00"},
{"-1", 0, "-1"}, {"-1", 0, "-1"},
{".25", 2, "0.25"}, {".25", 2, "0.25"},
{".25", 1, "0.3"}, {".25", 1, "0.3"},
{".25", 3, "0.250"},
{"-1/3", 3, "-0.333"}, {"-1/3", 3, "-0.333"},
{"-2/3", 4, "-0.6667"}, {"-2/3", 4, "-0.6667"},
{"0.96", 1, "1.0"}, {"0.96", 1, "1.0"},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment