Skip to content

Commit

Permalink
feat(decimal) : Add decimal support for calculations.
Browse files Browse the repository at this point in the history
Signed-off-by: TusharFA <tushar@flick2know.com>
  • Loading branch information
TusharFA committed Oct 24, 2024
1 parent 78a5832 commit 8fa6b3d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/src/utils/number_system_utils.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:decimal/decimal.dart';
import 'package:fa_dart_core/fa_dart_core.dart';

class NumberSystemUtils {
Expand Down Expand Up @@ -37,4 +38,27 @@ class NumberSystemUtils {
isCompact: isCompact,
);
}

/// Dart doesn't support operator overloading for now.
num decimalPlus(num first, num second) => num.parse(
(Decimal.parse(first.toString()) + Decimal.parse(second.toString()))
.toString());

num decimalSubtract(num first, num second) => num.parse(
(Decimal.parse(first.toString()) - Decimal.parse(second.toString()))
.toString());

num decimalMultiply(num first, num second) => num.parse(
(Decimal.parse(first.toString()) * Decimal.parse(second.toString()))
.toString());

num decimalDivide(num first, num second) {
if (second == 0 || second == 1) {
return first;
}
return num.parse(
(Decimal.parse(first.toString()) / Decimal.parse(second.toString()))
.toString());
}
}
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.3"
decimal:
dependency: "direct main"
description:
name: decimal
sha256: "4140a688f9e443e2f4de3a1162387bf25e1ac6d51e24c9da263f245210f41440"
url: "https://pub.dev"
source: hosted
version: "3.0.2"
fa_dart_logger:
dependency: "direct main"
description:
Expand Down Expand Up @@ -242,6 +250,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
rational:
dependency: transitive
description:
name: rational
sha256: cb808fb6f1a839e6fc5f7d8cb3b0a10e1db48b3be102de73938c627f0b636336
url: "https://pub.dev"
source: hosted
version: "2.2.3"
rxdart:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ environment:
dependencies:
rxdart: ^0.27.7
intl: ^0.19.0
decimal: ^3.0.2

# Logger
fa_dart_logger:
Expand Down

0 comments on commit 8fa6b3d

Please sign in to comment.