This commit is contained in:
Elirehema Paul 2024-02-19 21:37:10 -07:00 committed by GitHub
commit 6f9111fdab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 5 deletions

View file

@ -9,7 +9,7 @@
// //
// To use Decimal as part of a struct: // To use Decimal as part of a struct:
// //
// type StructName struct { // type Struct struct {
// Number Decimal // Number Decimal
// } // }
// //
@ -1042,6 +1042,13 @@ func (d Decimal) LessThanOrEqual(d2 Decimal) bool {
return cmp == -1 || cmp == 0 return cmp == -1 || cmp == 0
} }
// Betwee (LTE) return true when d is between d1 and d2 inclusive
func (d Decimal) IsBetween(d1 Decimal, d2 Decimal) bool {
cmp1 := d.Cmp(d1)
cmp2 := d.Cmp(d2)
return (cmp1 == 1 || cmp1 == 0) && (cmp2 == -1 || cmp2 == 0)
}
// Sign returns: // Sign returns:
// //
// -1 if d < 0 // -1 if d < 0
@ -1244,7 +1251,7 @@ func (d Decimal) Round(places int32) Decimal {
// NewFromFloat(545).RoundCeil(-2).String() // output: "600" // NewFromFloat(545).RoundCeil(-2).String() // output: "600"
// NewFromFloat(500).RoundCeil(-2).String() // output: "500" // NewFromFloat(500).RoundCeil(-2).String() // output: "500"
// NewFromFloat(1.1001).RoundCeil(2).String() // output: "1.11" // NewFromFloat(1.1001).RoundCeil(2).String() // output: "1.11"
// NewFromFloat(-1.454).RoundCeil(1).String() // output: "-1.4" // NewFromFloat(-1.454).RoundCeil(1).String() // output: "-1.5"
func (d Decimal) RoundCeil(places int32) Decimal { func (d Decimal) RoundCeil(places int32) Decimal {
if d.exp >= -places { if d.exp >= -places {
return d return d
@ -1269,7 +1276,7 @@ func (d Decimal) RoundCeil(places int32) Decimal {
// NewFromFloat(545).RoundFloor(-2).String() // output: "500" // NewFromFloat(545).RoundFloor(-2).String() // output: "500"
// NewFromFloat(-500).RoundFloor(-2).String() // output: "-500" // NewFromFloat(-500).RoundFloor(-2).String() // output: "-500"
// NewFromFloat(1.1001).RoundFloor(2).String() // output: "1.1" // NewFromFloat(1.1001).RoundFloor(2).String() // output: "1.1"
// NewFromFloat(-1.454).RoundFloor(1).String() // output: "-1.5" // NewFromFloat(-1.454).RoundFloor(1).String() // output: "-1.4"
func (d Decimal) RoundFloor(places int32) Decimal { func (d Decimal) RoundFloor(places int32) Decimal {
if d.exp >= -places { if d.exp >= -places {
return d return d
@ -1294,7 +1301,7 @@ func (d Decimal) RoundFloor(places int32) Decimal {
// NewFromFloat(545).RoundUp(-2).String() // output: "600" // NewFromFloat(545).RoundUp(-2).String() // output: "600"
// NewFromFloat(500).RoundUp(-2).String() // output: "500" // NewFromFloat(500).RoundUp(-2).String() // output: "500"
// NewFromFloat(1.1001).RoundUp(2).String() // output: "1.11" // NewFromFloat(1.1001).RoundUp(2).String() // output: "1.11"
// NewFromFloat(-1.454).RoundUp(1).String() // output: "-1.5" // NewFromFloat(-1.454).RoundUp(1).String() // output: "-1.4"
func (d Decimal) RoundUp(places int32) Decimal { func (d Decimal) RoundUp(places int32) Decimal {
if d.exp >= -places { if d.exp >= -places {
return d return d
@ -1321,7 +1328,7 @@ func (d Decimal) RoundUp(places int32) Decimal {
// NewFromFloat(545).RoundDown(-2).String() // output: "500" // NewFromFloat(545).RoundDown(-2).String() // output: "500"
// NewFromFloat(-500).RoundDown(-2).String() // output: "-500" // NewFromFloat(-500).RoundDown(-2).String() // output: "-500"
// NewFromFloat(1.1001).RoundDown(2).String() // output: "1.1" // NewFromFloat(1.1001).RoundDown(2).String() // output: "1.1"
// NewFromFloat(-1.454).RoundDown(1).String() // output: "-1.4" // NewFromFloat(-1.454).RoundDown(1).String() // output: "-1.5"
func (d Decimal) RoundDown(places int32) Decimal { func (d Decimal) RoundDown(places int32) Decimal {
if d.exp >= -places { if d.exp >= -places {
return d return d

View file

@ -2593,6 +2593,13 @@ func TestDecimal_Equalities(t *testing.T) {
if c.LessThanOrEqual(b) { if c.LessThanOrEqual(b) {
t.Errorf("%q should not be less than or equal %q", a, b) t.Errorf("%q should not be less than or equal %q", a, b)
} }
if !b.IsBetween(c, a) {
t.Errorf("%q should be less than or equal %q", a, b)
}
if b.IsBetween(a, c) {
t.Errorf("%q should not be less than or equal %q", a, b)
}
} }
func TestDecimal_ScalesNotEqual(t *testing.T) { func TestDecimal_ScalesNotEqual(t *testing.T) {