Skip to content

Commit

Permalink
add compareTo method (#1)
Browse files Browse the repository at this point in the history
* feat: add isNegative, one, two and compareTo.
  • Loading branch information
ciripel authored Feb 1, 2023
1 parent ed0dfd2 commit f09dcd2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.3

- Added compareTo method.
- Added isNegative property.
- Added BigDecimal.one and BigDecimal.two.
## 0.1.2

- Made implicit sign empty string.
Expand Down
19 changes: 19 additions & 0 deletions lib/src/sio_big_decimal_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,17 @@ class BigDecimal extends Equatable {
/// represents decimal place
final int precision;

static BigDecimal get one => BigDecimal.fromBigInt(BigInt.one);
static BigDecimal get two => BigDecimal.fromBigInt(BigInt.two);

bool get isZero =>
(_abs.isEmpty || (_abs[0] == 0 && _abs.length == 1)) && _dec == null;
bool get isNotZero => !isZero;
bool get isDecimal => _dec != null;
bool get isNotDecimal => !isDecimal;
bool get isFinite =>
_dec == null || (_dec != null && _dec!.length <= precision);
bool get isNegative => _sign == '-';

/// Addition operator.
///
Expand Down Expand Up @@ -356,6 +360,21 @@ class BigDecimal extends Equatable {
}
}

/// Compares this to `other`.
///
/// Returns a negative number if `this` is less than `other`, zero if they are
/// equal, and a positive number if `this` is greater than `other`.
///
/// Example:
/// ```dart
/// print(BigDecimal.one.compareTo(BigDecimal.two)); // => -1
/// print(BigDecimal.two.compareTo(BigDecimal.one)); // => 1
/// print(BigDecimal.one.compareTo(BigDecimal.one)); // => 0
/// ```
int compareTo(BigDecimal other) {
return toBigInt().compareTo(other.toBigInt());
}

BigDecimal clear() => BigDecimal.zero(precision: precision);

BigDecimal removeValue() {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sio_big_decimal
description: Numeric library to manipulate very big numbers keeping very high precision.
version: 0.1.2
version: 0.1.3
repository: https://github.com/SimplioOfficial/sio_big_decimal

environment:
Expand Down
17 changes: 17 additions & 0 deletions test/sio_big_decimal_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,21 @@ void main() {
);
});
});

group('BigDecimal getters tests -', () {
test('one', () {
expect(BigDecimal.one.toBigInt(), BigInt.one);
expect(BigDecimal.one.toString(), '1');
});
test('two', () {
expect(BigDecimal.two.toBigInt(), BigInt.two);
expect(BigDecimal.two.toString(), '2');
});
});

test('compareTo', () {
expect(BigDecimal.one.compareTo(BigDecimal.two), -1);
expect(BigDecimal.two.compareTo(BigDecimal.one), 1);
expect(BigDecimal.one.compareTo(BigDecimal.one), 0);
});
}

0 comments on commit f09dcd2

Please sign in to comment.