Skip to content

Commit

Permalink
Add GoString method for Decimal and NullDecimal (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
a69 committed Jan 8, 2022
1 parent fa3b22f commit c97d43b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,12 @@ func (d *Decimal) GobDecode(data []byte) error {
return d.UnmarshalBinary(data)
}

// GoString implements fmt.GoStringer and formats %#v to be printed in Go
// source code.
func (d *Decimal) GoString() string {
return `decimal.RequireFromString("` + d.String() + `")`
}

// StringScaled first scales the decimal then calls .String() on it.
// NOTE: buggy, unintuitive, and DEPRECATED! Use StringFixed instead.
func (d Decimal) StringScaled(exp int32) string {
Expand Down Expand Up @@ -1682,6 +1688,15 @@ func (d NullDecimal) MarshalText() (text []byte, err error) {
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

// Atan returns the arctangent, in radians, of x.
Expand Down

0 comments on commit c97d43b

Please sign in to comment.