diff --git a/CHANGELOG.md b/CHANGELOG.md index 2698bb6..d3c0224 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,14 +1,14 @@ +## 0.1.4 +- Added `<`,`<=`,`>`,`>=` operators. +- Added `abs` method. +- Implemented Comparable interface. ## 0.1.3 - -- Added compareTo method. -- Added isNegative property. -- Added BigDecimal.one and BigDecimal.two. +- Added `compareTo` method. +- Added `isNegative` property. +- Added `BigDecimal.one` and `BigDecimal.two`. ## 0.1.2 - - Made implicit sign empty string. ## 0.1.1 - -- Changed intl dependency version to ^0.17.0. +- Changed `intl` dependency version to `^0.17.0`. ## 0.1.0 - - Initial version. diff --git a/lib/src/sio_big_decimal_base.dart b/lib/src/sio_big_decimal_base.dart index ef92154..6b6bd00 100644 --- a/lib/src/sio_big_decimal_base.dart +++ b/lib/src/sio_big_decimal_base.dart @@ -4,7 +4,7 @@ import 'package:decimal/intl.dart'; import 'package:equatable/equatable.dart'; import 'package:intl/intl.dart'; -class BigDecimal extends Equatable { +class BigDecimal extends Equatable implements Comparable { static const defaultPrecision = 0; /// Create a new [BigDecimal] from a [value] of type [BigInt] given a @@ -371,10 +371,38 @@ class BigDecimal extends Equatable { /// print(BigDecimal.two.compareTo(BigDecimal.one)); // => 1 /// print(BigDecimal.one.compareTo(BigDecimal.one)); // => 0 /// ``` + @override int compareTo(BigDecimal other) { return toBigInt().compareTo(other.toBigInt()); } + /// Whether this [BigDecimal] is numerically smaller than [other]. + bool operator <(BigDecimal other) => compareTo(other) < 0; + + /// Whether this [BigDecimal] is numerically smaller than or equal to [other]. + bool operator <=(BigDecimal other) => compareTo(other) <= 0; + + /// Whether this [BigDecimal] is numerically greater than [other]. + bool operator >(BigDecimal other) => compareTo(other) > 0; + + /// Whether this [BigDecimal] is numerically greater than or equal to [other]. + bool operator >=(BigDecimal other) => compareTo(other) >= 0; + + /// Returns the absolute value of this [BigDecimal]. + /// + /// For any [BigDecimal] `x`, the result is the same as `x < 0 ? -x : x`. + BigDecimal abs() => _copyWith(sign: ''); + + /// Return the negative value of this [BigDecimal]. + /// + /// The result of negating a [BigDecimal] always has the opposite sign, + /// except for zero, which is its own negation. + BigDecimal operator -() => isZero + ? _copyWith(sign: '') + : isNegative + ? _copyWith(sign: '') + : _copyWith(sign: '-'); + BigDecimal clear() => BigDecimal.zero(precision: precision); BigDecimal removeValue() { @@ -466,5 +494,5 @@ class BigDecimal extends Equatable { } @override - List get props => [_abs, _dec]; + List get props => [_sign, _abs, _dec]; } diff --git a/pubspec.yaml b/pubspec.yaml index c36a9eb..c169d25 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: sio_big_decimal description: Numeric library to manipulate very big numbers keeping very high precision. -version: 0.1.3 +version: 0.1.4 repository: https://github.com/SimplioOfficial/sio_big_decimal environment: @@ -12,4 +12,4 @@ dev_dependencies: dependencies: decimal: ^2.3.2 equatable: ^2.0.5 - intl: ^0.17.0 + intl: ^0.18.0 diff --git a/test/sio_big_decimal_test.dart b/test/sio_big_decimal_test.dart index 2bb1cd0..f3c6764 100644 --- a/test/sio_big_decimal_test.dart +++ b/test/sio_big_decimal_test.dart @@ -60,6 +60,41 @@ void main() { }); group('BigDecimal operators tests -', () { + test('smaller than (<)', () { + expect(BigDecimal.one < BigDecimal.two, true); + expect(BigDecimal.zero() < BigDecimal.zero(precision: 10), false); + expect(BigDecimal.parse('30.00') < BigDecimal.parse('30'), false); + expect(BigDecimal.parse('10') < BigDecimal.parse('-10'), false); + expect(BigDecimal.zero() < BigDecimal.parse('-0'), false); + }); + test('smaller than or equal (<=)', () { + expect(BigDecimal.one <= BigDecimal.two, true); + expect(BigDecimal.zero() <= BigDecimal.zero(precision: 10), true); + expect(BigDecimal.parse('30.00') <= BigDecimal.parse('30'), true); + expect(BigDecimal.parse('10') <= BigDecimal.parse('-10'), false); + expect(BigDecimal.zero() <= BigDecimal.parse('-0'), true); + }); + test('greater than (>)', () { + expect(BigDecimal.one > BigDecimal.two, false); + expect(BigDecimal.zero() > BigDecimal.zero(precision: 10), false); + expect(BigDecimal.parse('30.00') > BigDecimal.parse('30'), false); + expect(BigDecimal.parse('10') > BigDecimal.parse('-10'), true); + expect(BigDecimal.zero() > BigDecimal.parse('-0'), false); + }); + test('smaller than or equal (>=)', () { + expect(BigDecimal.one >= BigDecimal.two, false); + expect(BigDecimal.zero() >= BigDecimal.zero(precision: 10), true); + expect(BigDecimal.parse('30.00') >= BigDecimal.parse('30'), true); + expect(BigDecimal.parse('10') >= BigDecimal.parse('-10'), true); + expect(BigDecimal.zero() >= BigDecimal.parse('-0'), true); + }); + + test('negation -()', () { + expect((-BigDecimal.one), BigDecimal.parse('-1')); + expect(-BigDecimal.parse('-1'), BigDecimal.one); + expect(-BigDecimal.zero(), BigDecimal.zero()); + }); + test('addition (+)', () { expect( (BigDecimal.parse('1.222', precision: 3) + @@ -241,6 +276,11 @@ void main() { '10.1019294878270532377', ); }); + test('abs', () { + expect(BigDecimal.one.abs(), BigDecimal.one); + expect((-BigDecimal.one).abs(), BigDecimal.one); + expect(BigDecimal.parse('-1').abs(), BigDecimal.one); + }); }); group('BigDecimal getters tests -', () {