Add Neg method (#48)

Neg provides an easy way to negate a Decimal.
This commit is contained in:
Evan Shaw 2017-02-22 07:39:36 +13:00 committed by Victor Quinn
parent 77e6b496f6
commit 4b9150b187
2 changed files with 30 additions and 0 deletions

View file

@ -262,6 +262,15 @@ func (d Decimal) Sub(d2 Decimal) Decimal {
} }
} }
// Neg returns -d.
func (d Decimal) Neg() Decimal {
val := new(big.Int).Neg(d.value)
return Decimal{
value: val,
exp: d.exp,
}
}
// Mul returns d * d2. // Mul returns d * d2.
func (d Decimal) Mul(d2 Decimal) Decimal { func (d Decimal) Mul(d2 Decimal) Decimal {
d.ensureInitialized() d.ensureInitialized()

View file

@ -620,6 +620,27 @@ func TestDecimal_Sub(t *testing.T) {
} }
} }
func TestDecimal_Neg(t *testing.T) {
inputs := map[string]string{
"0": "0",
"10": "-10",
"5.56": "-5.56",
"-10": "10",
"-5.56": "5.56",
}
for inp, res := range inputs {
a, err := NewFromString(inp)
if err != nil {
t.FailNow()
}
b := a.Neg()
if b.String() != res {
t.Errorf("expected %s, got %s", res, b.String())
}
}
}
func TestDecimal_Mul(t *testing.T) { func TestDecimal_Mul(t *testing.T) {
type Inp struct { type Inp struct {
a string a string