github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/math/big/floatmarsh.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This file implements encoding/decoding of Floats.
     6  
     7  package big
     8  
     9  import "fmt"
    10  
    11  // MarshalText implements the encoding.TextMarshaler interface.
    12  // Only the Float value is marshaled (in full precision), other
    13  // attributes such as precision or accuracy are ignored.
    14  func (x *Float) MarshalText() (text []byte, err error) {
    15  	if x == nil {
    16  		return []byte("<nil>"), nil
    17  	}
    18  	var buf []byte
    19  	return x.Append(buf, 'g', -1), nil
    20  }
    21  
    22  // UnmarshalText implements the encoding.TextUnmarshaler interface.
    23  // The result is rounded per the precision and rounding mode of z.
    24  // If z's precision is 0, it is changed to 64 before rounding takes
    25  // effect.
    26  func (z *Float) UnmarshalText(text []byte) error {
    27  	// TODO(gri): get rid of the []byte/string conversion
    28  	_, _, err := z.Parse(string(text), 0)
    29  	if err != nil {
    30  		err = fmt.Errorf("math/big: cannot unmarshal %q into a *big.Float (%v)", text, err)
    31  	}
    32  	return err
    33  }