From 8fa6b3dc2ecd8b4787d1bd9033262d683536e489 Mon Sep 17 00:00:00 2001 From: TusharFA Date: Thu, 24 Oct 2024 14:20:59 +0530 Subject: [PATCH] feat(decimal) : Add decimal support for calculations. Signed-off-by: TusharFA --- lib/src/utils/number_system_utils.dart | 24 ++++++++++++++++++++++++ pubspec.lock | 16 ++++++++++++++++ pubspec.yaml | 1 + 3 files changed, 41 insertions(+) diff --git a/lib/src/utils/number_system_utils.dart b/lib/src/utils/number_system_utils.dart index b46a514..2c506e5 100644 --- a/lib/src/utils/number_system_utils.dart +++ b/lib/src/utils/number_system_utils.dart @@ -1,3 +1,4 @@ +import 'package:decimal/decimal.dart'; import 'package:fa_dart_core/fa_dart_core.dart'; class NumberSystemUtils { @@ -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()); + } } diff --git a/pubspec.lock b/pubspec.lock index 0ddb9d8..3e1828e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: @@ -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: diff --git a/pubspec.yaml b/pubspec.yaml index 85a63bc..77ea5ee 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,6 +13,7 @@ environment: dependencies: rxdart: ^0.27.7 intl: ^0.19.0 + decimal: ^3.0.2 # Logger fa_dart_logger: