aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRigel <rigel.rozanski@gmail.com>2017-05-18 06:01:17 +0800
committerVictor Quinn <mail@victorquinn.com>2017-05-18 06:01:17 +0800
commit16a941821474ee3986fdbeab535a68a8aa5a85d2 (patch)
treef8523583f9153913792b60d3a73f06b698e76b3e
parentd5fda14026cb2f519715d2714468043fa0ace48e (diff)
downloaddexon-decimal-16a941821474ee3986fdbeab535a68a8aa5a85d2.tar.gz
dexon-decimal-16a941821474ee3986fdbeab535a68a8aa5a85d2.tar.zst
dexon-decimal-16a941821474ee3986fdbeab535a68a8aa5a85d2.zip
added GT, GTE, LT, LTE (#54)
* added GT, GTE, LT, LTE * GTE -> GreaterThanOrEqual
-rw-r--r--decimal.go22
-rw-r--r--decimal_test.go39
2 files changed, 59 insertions, 2 deletions
diff --git a/decimal.go b/decimal.go
index cf0f3ce..c4467a9 100644
--- a/decimal.go
+++ b/decimal.go
@@ -423,6 +423,28 @@ func (d Decimal) Equals(d2 Decimal) bool {
return d.Equal(d2)
}
+// Greater Than (GT) returns true when d is greater than d2.
+func (d Decimal) GreaterThan(d2 Decimal) bool {
+ return d.Cmp(d2) == 1
+}
+
+// Greater Than or Equal (GTE) returns true when d is greater than or equal to d2.
+func (d Decimal) GreaterThanOrEqual(d2 Decimal) bool {
+ cmp := d.Cmp(d2)
+ return cmp == 1 || cmp == 0
+}
+
+// Less Than (LT) returns true when d is less than d2.
+func (d Decimal) LessThan(d2 Decimal) bool {
+ return d.Cmp(d2) == -1
+}
+
+// Less Than or Equal (LTE) returns true when d is less than or equal to d2.
+func (d Decimal) LessThanOrEqual(d2 Decimal) bool {
+ cmp := d.Cmp(d2)
+ return cmp == -1 || cmp == 0
+}
+
// Sign returns:
//
// -1 if d < 0
diff --git a/decimal_test.go b/decimal_test.go
index 96f7340..358609d 100644
--- a/decimal_test.go
+++ b/decimal_test.go
@@ -1322,18 +1322,53 @@ func TestDecimal_Abs2(t *testing.T) {
}
}
-func TestDecimal_Equal(t *testing.T) {
+func TestDecimal_Equalities(t *testing.T) {
a := New(1234, 3)
b := New(1234, 3)
+ c := New(1234, 4)
if !a.Equal(b) {
t.Errorf("%q should equal %q", a, b)
}
+ if a.Equal(c) {
+ t.Errorf("%q should not equal %q", a, c)
+ }
- // note, this should be deprecated, here for backwards compatibility
+ // note, this block should be deprecated, here for backwards compatibility
if !a.Equals(b) {
t.Errorf("%q should equal %q", a, b)
}
+
+ if !c.GreaterThan(b) {
+ t.Errorf("%q should be greater than %q", c, b)
+ }
+ if b.GreaterThan(c) {
+ t.Errorf("%q should not be greater than %q", b, c)
+ }
+ if !a.GreaterThanOrEqual(b) {
+ t.Errorf("%q should be greater or equal %q", a, b)
+ }
+ if !c.GreaterThanOrEqual(b) {
+ t.Errorf("%q should be greater or equal %q", c, b)
+ }
+ if b.GreaterThanOrEqual(c) {
+ t.Errorf("%q should not be greater or equal %q", b, c)
+ }
+ if !b.LessThan(c) {
+ t.Errorf("%q should be less than %q", a, b)
+ }
+ if c.LessThan(b) {
+ t.Errorf("%q should not be less than %q", a, b)
+ }
+ if !a.LessThanOrEqual(b) {
+ t.Errorf("%q should be less than or equal %q", a, b)
+ }
+ if !b.LessThanOrEqual(c) {
+ t.Errorf("%q should be less than or equal %q", a, b)
+ }
+ if c.LessThanOrEqual(b) {
+ t.Errorf("%q should not be less than or equal %q", a, b)
+ }
}
func TestDecimal_ScalesNotEqual(t *testing.T) {