diff --git a/lib/utilities/amount.dart b/lib/utilities/amount.dart index 74d37b6b5..24d7b507c 100644 --- a/lib/utilities/amount.dart +++ b/lib/utilities/amount.dart @@ -12,6 +12,12 @@ class Amount implements Equatable { }) : assert(fractionDigits >= 0), _value = rawValue; + /// special zero case with [fractionDigits] set to 0 + static Amount get zero => Amount( + rawValue: BigInt.zero, + fractionDigits: 0, + ); + /// truncate double value to [fractionDigits] places Amount.fromDouble(double amount, {required this.fractionDigits}) : assert(fractionDigits >= 0), @@ -66,6 +72,17 @@ class Amount implements Equatable { ); } + // =========================================================================== + // ======= operators ========================================================= + + bool operator >(Amount other) => raw > other.raw; + + bool operator <(Amount other) => raw < other.raw; + + bool operator >=(Amount other) => raw >= other.raw; + + bool operator <=(Amount other) => raw <= other.raw; + // =========================================================================== // ======= Overrides ========================================================= diff --git a/test/utilities/amount_test.dart b/test/utilities/amount_test.dart index 0274ea2bb..882055b3f 100644 --- a/test/utilities/amount_test.dart +++ b/test/utilities/amount_test.dart @@ -78,4 +78,41 @@ void main() { } expect(didThrow, true); }); + + group("operators", () { + final one = Amount(rawValue: BigInt.one, fractionDigits: 0); + final two = Amount(rawValue: BigInt.two, fractionDigits: 0); + + test(">", () { + expect(one > two, false); + expect(one > one, false); + + expect(two > two, false); + expect(two > one, true); + }); + + test("<", () { + expect(one < two, true); + expect(one < one, false); + + expect(two < two, false); + expect(two < one, false); + }); + + test(">=", () { + expect(one >= two, false); + expect(one >= one, true); + + expect(two >= two, true); + expect(two >= one, true); + }); + + test("<=", () { + expect(one <= two, true); + expect(one <= one, true); + + expect(two <= two, true); + expect(two <= one, false); + }); + }); }