mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 12:30:49 +01:00
Add Sign method
d.Sign() is a more convenient way of saying d.Cmp(Zero).
This commit is contained in:
parent
5471f2c322
commit
1aec7720a8
2 changed files with 32 additions and 0 deletions
13
decimal.go
13
decimal.go
|
@ -411,6 +411,19 @@ func (d Decimal) Equals(d2 Decimal) bool {
|
|||
return d.Equal(d2)
|
||||
}
|
||||
|
||||
// Sign returns:
|
||||
//
|
||||
// -1 if d < 0
|
||||
// 0 if d == 0
|
||||
// +1 if d > 0
|
||||
//
|
||||
func (d Decimal) Sign() int {
|
||||
if d.value == nil {
|
||||
return 0
|
||||
}
|
||||
return d.value.Sign()
|
||||
}
|
||||
|
||||
// Exponent returns the exponent, or scale component of the decimal.
|
||||
func (d Decimal) Exponent() int32 {
|
||||
return d.exp
|
||||
|
|
|
@ -506,6 +506,9 @@ func TestDecimal_Uninitialized(t *testing.T) {
|
|||
if a.Cmp(b) != 0 {
|
||||
t.Errorf("a != b")
|
||||
}
|
||||
if a.Sign() != 0 {
|
||||
t.Errorf("a.Sign() != 0")
|
||||
}
|
||||
if a.Exponent() != 0 {
|
||||
t.Errorf("a.Exponent() != 0")
|
||||
}
|
||||
|
@ -1292,6 +1295,22 @@ func TestPow(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestDecimal_Sign(t *testing.T) {
|
||||
if Zero.Sign() != 0 {
|
||||
t.Errorf("%q should have sign 0", Zero)
|
||||
}
|
||||
|
||||
one := New(1, 0)
|
||||
if one.Sign() != 1 {
|
||||
t.Errorf("%q should have sign 1", one)
|
||||
}
|
||||
|
||||
mone := New(-1, 0)
|
||||
if mone.Sign() != -1 {
|
||||
t.Errorf("%q should have sign -1", mone)
|
||||
}
|
||||
}
|
||||
|
||||
func didPanic(f func()) bool {
|
||||
ret := false
|
||||
func() {
|
||||
|
|
Loading…
Reference in a new issue