Skip to content

Commit

Permalink
implement more operators
Browse files Browse the repository at this point in the history
- add <,<=,>,>= operators.
- add abs method.
- implement Comparable interface.
  • Loading branch information
ciripel authored Feb 2, 2023
1 parent f09dcd2 commit 076d726
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
16 changes: 8 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
32 changes: 30 additions & 2 deletions lib/src/sio_big_decimal_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<BigDecimal> {
static const defaultPrecision = 0;

/// Create a new [BigDecimal] from a [value] of type [BigInt] given a
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -466,5 +494,5 @@ class BigDecimal extends Equatable {
}

@override
List<Object?> get props => [_abs, _dec];
List<Object?> get props => [_sign, _abs, _dec];
}
4 changes: 2 additions & 2 deletions 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.3
version: 0.1.4
repository: https://github.com/SimplioOfficial/sio_big_decimal

environment:
Expand All @@ -12,4 +12,4 @@ dev_dependencies:
dependencies:
decimal: ^2.3.2
equatable: ^2.0.5
intl: ^0.17.0
intl: ^0.18.0
40 changes: 40 additions & 0 deletions test/sio_big_decimal_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) +
Expand Down Expand Up @@ -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 -', () {
Expand Down

0 comments on commit 076d726

Please sign in to comment.