From 57a340d853bea1bc75bb82a29402cc2de279c49a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Tudor=20C=C4=83lin?= Date: Thu, 25 Jan 2024 00:48:15 +0200 Subject: [PATCH] Add Compare method which forwards calls to Cmp (#346) Given the interface definition type Ordered[T any] interface { Compare(T) int } And the type constraint T Ordered[T], make decimal.Decimal satisfy this constraint, so that generic code written against T Ordered[T] can work with decimal values as smoothly as it works with time.Time values today. Fixes: #345 --- decimal.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/decimal.go b/decimal.go index 8677533..e73b379 100644 --- a/decimal.go +++ b/decimal.go @@ -981,6 +981,15 @@ func (d Decimal) Cmp(d2 Decimal) int { return rd.value.Cmp(rd2.value) } +// Compare compares the numbers represented by d and d2 and returns: +// +// -1 if d < d2 +// 0 if d == d2 +// +1 if d > d2 +func (d Decimal) Compare(d2 Decimal) int { + return d.Cmp(d2) +} + // Equal returns whether the numbers represented by d and d2 are equal. func (d Decimal) Equal(d2 Decimal) bool { return d.Cmp(d2) == 0