Skip to content

Commit

Permalink
✨ Add decimal property in toBigInt and fromBigInt method
Browse files Browse the repository at this point in the history
  • Loading branch information
redDwarf03 committed Apr 16, 2024
1 parent eb19dac commit 2a3016d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ Changelog
=========
=========

#### Version 3.3.13
* Add decimal property in `toBigInt` and `fromBigInt` method

#### Version 3.3.12
* Fix `ArchethicJsonRPCException` is not catched

Expand Down
17 changes: 10 additions & 7 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,26 @@ Uint8List concatUint8List(Iterable<Uint8List> list) =>

/// Convert any number into a big int for 10^8 decimals
/// @param {num} Number to convert
int toBigInt(num? number) {
/// @param {num} Number to of decimals
int toBigInt(num? number, {int decimal = 8}) {
if (number == null) {
return 0;
}
return (Decimal.parse(number.toString()) * Decimal.parse('100000000'))
.toDouble()
.floor();
var nb = number;
for (var i = 0; i < decimal; i++) {
nb *= 10;
}
return nb.truncate();
}

/// Convert big int of 10^8 decimals to any number
/// @param {int} Number to convert
num fromBigInt(int? number) {
num fromBigInt(int? number, {int decimal = 8}) {
if (number == null) {
return 0;
}
return (Decimal.parse(number.toString()) / Decimal.parse('100000000'))
.toDouble();
final divisor = Decimal.parse('10').pow(decimal).toDecimal();
return (Decimal.parse(number.toString()) / divisor).toDouble();
}

/// Convert any number into a byte array
Expand Down
3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: archethic_lib_dart
description: Archethic dart library for Flutter for Node and Browser. This library aims to provide a easy way to create Archethic transaction and to send them over the network
homepage: https://github.com/archethic-foundation/libdart

version: 3.3.12
version: 3.3.13

environment:
sdk: ">=3.3.0 <4.0.0"
Expand Down Expand Up @@ -60,7 +60,6 @@ dev_dependencies:

# A tool to help you find missing, under-promoted, over-promoted, and unused dependencies.
dependency_validator: ^3.2.3

# Code generation for immutable classes that has a simple syntax/API without compromising on the features
freezed: ^2.4.7

Expand Down
16 changes: 16 additions & 0 deletions test/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ void main() {
test('should convert a double into a big integer with 10^8', () {
expect(toBigInt(0.617714895), 61771489);
});

test('should return Big Int with 8 decimals by default', () {
expect(toBigInt(12.5345), 1253450000);
});

test('should return Big Int with decimals passed in param', () {
expect(toBigInt(12.5345, decimal: 6), 12534500);
});
});

group('fromBigInt', () {
Expand All @@ -70,6 +78,14 @@ void main() {
test('should convert a big integer with 10^8 into double', () {
expect(0.61771489, fromBigInt(61771489));
});

test('should return 8 decimals number by default', () {
expect(12.5345, fromBigInt(1253450000));
});

test('should return decimals number with decimals passed in param', () {
expect(12.5345, fromBigInt(12534500, decimal: 6));
});
});

group('toByteArray', () {
Expand Down

0 comments on commit 2a3016d

Please sign in to comment.