Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ func (d Decimal) Cmp(other Decimal) int {
return d.fl.Cmp(other.fl)
}

// Max will return the max Decimal between two Decimals
func (d Decimal) Max(other Decimal) Decimal {
if d.GTE(other) {
return d
}

return other
}

// Min will return the min Decimal between two Decimals
func (d Decimal) Min(other Decimal) Decimal {
if d.LTE(other) {
return d
}

return other
}

// Float will return this Decimal as a float value.
// Note that there may be some loss of precision in this operation.
func (d Decimal) Float() float64 {
Expand Down
47 changes: 47 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,50 @@ func TestDecimal_Sql(t *testing.T) {
assert.EqualValues(t, "Passed value 1.23 should be a string", err.Error())
})
}

func TestDecimal_Max(t *testing.T) {
t.Run("a > b", func(t *testing.T) {
a := NewDecimal(2)
b := NewDecimal(1)

assert.Equal(t, a.Max(b), a)
})

t.Run("a = b", func(t *testing.T) {

a := NewDecimal(1)
b := NewDecimal(1)

assert.Equal(t, a.Max(b), a)
})

t.Run("a < b", func(t *testing.T) {
a := NewDecimal(1)
b := NewDecimal(2)

assert.Equal(t, a.Max(b), b)
})
}

func TestDecimal_Min(t *testing.T) {
t.Run("a > b", func(t *testing.T) {
a := NewDecimal(2)
b := NewDecimal(1)

assert.Equal(t, a.Min(b), b)
})

t.Run("a = b", func(t *testing.T) {
a := NewDecimal(1)
b := NewDecimal(1)

assert.Equal(t, a.Min(b), a)
})

t.Run("a < b", func(t *testing.T) {
a := NewDecimal(1)
b := NewDecimal(2)

assert.Equal(t, a.Min(b), a)
})
}