Skip to content

Commit 5d3a982

Browse files
committed
Add GoString method for Decimal and NullDecimal (#270)
1 parent fa3b22f commit 5d3a982

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

decimal.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,19 @@ func (d *Decimal) GobDecode(data []byte) error {
14541454
return d.UnmarshalBinary(data)
14551455
}
14561456

1457+
// GoString implements fmt.GoStringer and formats %#v to be printed in Go
1458+
// source code.
1459+
func (d *Decimal) GoString() string {
1460+
1461+
if d.value.IsInt64() {
1462+
return fmt.Sprintf("decimal.New(%d,%d)", d.value.Int64(), d.exp)
1463+
}
1464+
if d.value.Sign() < 0 {
1465+
return fmt.Sprintf("decimal.NewFromBigInt(new(big.Int).Neg(new(big.Int).SetBits(%#v)),%d)", d.value.Bits(), d.exp)
1466+
}
1467+
return fmt.Sprintf("decimal.NewFromBigInt(new(big.Int).SetBits(%#v),%d)", d.value.Bits(), d.exp)
1468+
}
1469+
14571470
// StringScaled first scales the decimal then calls .String() on it.
14581471
// NOTE: buggy, unintuitive, and DEPRECATED! Use StringFixed instead.
14591472
func (d Decimal) StringScaled(exp int32) string {
@@ -1682,6 +1695,15 @@ func (d NullDecimal) MarshalText() (text []byte, err error) {
16821695
return d.Decimal.MarshalText()
16831696
}
16841697

1698+
// GoString implements fmt.GoStringer and formats %#v to be printed in Go
1699+
// source code.
1700+
func (d NullDecimal) GoString() string {
1701+
if !d.Valid {
1702+
return "decimal.NullDecimal{Valid: false}"
1703+
}
1704+
return `decimal.NewNullDecimal(` + d.Decimal.GoString() + `)`
1705+
}
1706+
16851707
// Trig functions
16861708

16871709
// Atan returns the arctangent, in radians, of x.

0 commit comments

Comments
 (0)