mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 04:20:50 +01:00
Add GoString method for Decimal and NullDecimal (#270)
This commit is contained in:
parent
fa3b22f4d4
commit
5d3a9828f2
1 changed files with 22 additions and 0 deletions
22
decimal.go
22
decimal.go
|
@ -1454,6 +1454,19 @@ func (d *Decimal) GobDecode(data []byte) error {
|
||||||
return d.UnmarshalBinary(data)
|
return d.UnmarshalBinary(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GoString implements fmt.GoStringer and formats %#v to be printed in Go
|
||||||
|
// source code.
|
||||||
|
func (d *Decimal) GoString() string {
|
||||||
|
|
||||||
|
if d.value.IsInt64() {
|
||||||
|
return fmt.Sprintf("decimal.New(%d,%d)", d.value.Int64(), d.exp)
|
||||||
|
}
|
||||||
|
if d.value.Sign() < 0 {
|
||||||
|
return fmt.Sprintf("decimal.NewFromBigInt(new(big.Int).Neg(new(big.Int).SetBits(%#v)),%d)", d.value.Bits(), d.exp)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("decimal.NewFromBigInt(new(big.Int).SetBits(%#v),%d)", d.value.Bits(), d.exp)
|
||||||
|
}
|
||||||
|
|
||||||
// StringScaled first scales the decimal then calls .String() on it.
|
// StringScaled first scales the decimal then calls .String() on it.
|
||||||
// NOTE: buggy, unintuitive, and DEPRECATED! Use StringFixed instead.
|
// NOTE: buggy, unintuitive, and DEPRECATED! Use StringFixed instead.
|
||||||
func (d Decimal) StringScaled(exp int32) string {
|
func (d Decimal) StringScaled(exp int32) string {
|
||||||
|
@ -1682,6 +1695,15 @@ func (d NullDecimal) MarshalText() (text []byte, err error) {
|
||||||
return d.Decimal.MarshalText()
|
return d.Decimal.MarshalText()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GoString implements fmt.GoStringer and formats %#v to be printed in Go
|
||||||
|
// source code.
|
||||||
|
func (d NullDecimal) GoString() string {
|
||||||
|
if !d.Valid {
|
||||||
|
return "decimal.NullDecimal{Valid: false}"
|
||||||
|
}
|
||||||
|
return `decimal.NewNullDecimal(` + d.Decimal.GoString() + `)`
|
||||||
|
}
|
||||||
|
|
||||||
// Trig functions
|
// Trig functions
|
||||||
|
|
||||||
// Atan returns the arctangent, in radians, of x.
|
// Atan returns the arctangent, in radians, of x.
|
||||||
|
|
Loading…
Reference in a new issue