From cdfde59befd07be10741ce4b0c8dbd561b14502d Mon Sep 17 00:00:00 2001 From: ciripel <37701673+ciripel@users.noreply.github.com> Date: Fri, 17 Feb 2023 23:07:35 +0200 Subject: [PATCH] Fix/revert zero() (#5) --- CHANGELOG.md | 10 ++++++---- example/sio_big_decimal_example.dart | 6 +++++- lib/src/sio_big_decimal_base.dart | 15 ++++++++++++++- pubspec.yaml | 2 +- test/sio_big_decimal_test.dart | 21 +++++++++++---------- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2894c68..a1cc117 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ +## 0.2.1 +- Revert `zero()` cause `const` is very valuable. ## 0.2.0 -- Changed `zero()` to `zero` -- Fixed bug in `isZero` -- Refactor `clear()` +- Changed `zero()` to `zero`. +- Fixed bug in `isZero`. +- Refactor `clear()`. ## 0.1.5 -- Fixed bug in compareTo. +- Fixed bug in `compareTo`. ## 0.1.4 - Added `<`,`<=`,`>`,`>=` operators. - Added `abs` method. diff --git a/example/sio_big_decimal_example.dart b/example/sio_big_decimal_example.dart index 87379ce..6f15ff3 100644 --- a/example/sio_big_decimal_example.dart +++ b/example/sio_big_decimal_example.dart @@ -48,10 +48,14 @@ void main() { print(e); } print('=================================================='); + print(BigDecimal.zero()); // 0 + print(BigDecimal.zero(precision: 8)); // 0.00000000 + print(BigDecimal.zero(precision: -1)); // 0 + print('=================================================='); final x = BigDecimal.parse('1.222', precision: 3); final y = BigDecimal.parse('0.2225', precision: 4); final z = BigDecimal.parse('1.888', precision: 4); - final zero = BigDecimal.zero; + final zero = BigDecimal.zero(); final addition = x + z; // 3.11 (precision 2) final subtract = x - y; // 0.9995 (precision 4) final multiplication = x * z; // 2.307136 (precision: 6) diff --git a/lib/src/sio_big_decimal_base.dart b/lib/src/sio_big_decimal_base.dart index 9182f21..1469236 100644 --- a/lib/src/sio_big_decimal_base.dart +++ b/lib/src/sio_big_decimal_base.dart @@ -125,7 +125,20 @@ class BigDecimal extends Equatable implements Comparable { final int precision; /// The [BigDecimal] corresponding to `0`. - static BigDecimal get zero => BigDecimal.fromBigInt(BigInt.zero); + /// + /// [defaultPrecision] is 0. + /// + /// Negative [precision] will default to `0`. + /// + /// Example: + /// ```dart + /// print(BigDecimal.zero()); // 0 + /// print(BigDecimal.zero(precision: 8)); // 0.00000000 + /// print(BigDecimal.zero(precision: -1)); // 0 + /// ``` + const BigDecimal.zero({ + int precision = defaultPrecision, + }) : this._('', const [0], null, precision); /// The [BigDecimal] corresponding to `1`. static BigDecimal get one => BigDecimal.fromBigInt(BigInt.one); diff --git a/pubspec.yaml b/pubspec.yaml index 9f6ef59..61deb3f 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.2.0 +version: 0.2.1 repository: https://github.com/SimplioOfficial/sio_big_decimal environment: diff --git a/test/sio_big_decimal_test.dart b/test/sio_big_decimal_test.dart index fce4db0..a6f2107 100644 --- a/test/sio_big_decimal_test.dart +++ b/test/sio_big_decimal_test.dart @@ -53,36 +53,42 @@ void main() { }); }); + test('zero', () { + expect(BigDecimal.zero().toString(), '0'); + expect(BigDecimal.zero(precision: 8).toString(), '0.00000000'); + expect(BigDecimal.zero(precision: -1).toString(), '0'); + }); + group('BigDecimal operators tests -', () { test('smaller than (<)', () { expect(BigDecimal.one < BigDecimal.two, true); expect(BigDecimal.parse('30.00') < BigDecimal.parse('30'), false); expect(BigDecimal.parse('10') < BigDecimal.parse('-10'), false); - expect(BigDecimal.zero < BigDecimal.parse('-0'), false); + expect(BigDecimal.zero() < BigDecimal.parse('-0'), false); }); test('smaller than or equal (<=)', () { expect(BigDecimal.one <= BigDecimal.two, 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); + expect(BigDecimal.zero() <= BigDecimal.parse('-0'), true); }); test('greater than (>)', () { expect(BigDecimal.one > BigDecimal.two, 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); + expect(BigDecimal.zero() > BigDecimal.parse('-0'), false); }); test('smaller than or equal (>=)', () { expect(BigDecimal.one >= BigDecimal.two, false); expect(BigDecimal.parse('30.00') >= BigDecimal.parse('30'), true); expect(BigDecimal.parse('10') >= BigDecimal.parse('-10'), true); - expect(BigDecimal.zero >= BigDecimal.parse('-0'), 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); + expect(-BigDecimal.zero(), BigDecimal.zero()); }); test('addition (+)', () { @@ -274,11 +280,6 @@ void main() { }); group('BigDecimal getters tests -', () { - test('zero', () { - expect(BigDecimal.zero.toBigInt(), BigInt.zero); - expect(BigDecimal.zero.toString(), '0'); - }); - test('one', () { expect(BigDecimal.one.toBigInt(), BigInt.one); expect(BigDecimal.one.toString(), '1');