diff options
author | Robert Griesemer <gri@golang.org> | 2015-09-25 16:35:54 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2015-09-29 00:21:45 +0000 |
commit | 38c5fd5cf872cf2eabad1361342097e11d292c91 (patch) | |
tree | dda8a302101fca4eafc1119bcddc782b5434749c /src/math/big/floatmarsh.go | |
parent | 02e8ec008ca80e6b7dd93410aa9abac3a906dee4 (diff) | |
download | go-38c5fd5cf872cf2eabad1361342097e11d292c91.tar.gz go-38c5fd5cf872cf2eabad1361342097e11d292c91.zip |
math/big: implement Float.Text(Un)Marshaler
Fixes #12256.
Change-Id: Ie4a3337996da5c060b27530b076048ffead85f3b
Reviewed-on: https://go-review.googlesource.com/15040
Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/math/big/floatmarsh.go')
-rw-r--r-- | src/math/big/floatmarsh.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/math/big/floatmarsh.go b/src/math/big/floatmarsh.go new file mode 100644 index 0000000000..44987ee03a --- /dev/null +++ b/src/math/big/floatmarsh.go @@ -0,0 +1,33 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file implements encoding/decoding of Floats. + +package big + +import "fmt" + +// MarshalText implements the encoding.TextMarshaler interface. +// Only the Float value is marshaled (in full precision), other +// attributes such as precision or accuracy are ignored. +func (x *Float) MarshalText() (text []byte, err error) { + if x == nil { + return []byte("<nil>"), nil + } + var buf []byte + return x.Append(buf, 'g', -1), nil +} + +// UnmarshalText implements the encoding.TextUnmarshaler interface. +// The result is rounded per the precision and rounding mode of z. +// If z's precision is 0, it is changed to 64 before rounding takes +// effect. +func (z *Float) UnmarshalText(text []byte) error { + // TODO(gri): get rid of the []byte/string conversion + _, _, err := z.Parse(string(text), 0) + if err != nil { + err = fmt.Errorf("math/big: cannot unmarshal %q into a *big.Float (%v)", text, err) + } + return err +} |