Skip to content

Commit

Permalink
Merge pull request #4 from bugless/quick-conversions
Browse files Browse the repository at this point in the history
Quick conversions
  • Loading branch information
gusbicalho authored Jul 16, 2021
2 parents bee976a + 049a16e commit 088ebf6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 0.2.0
- Implement and test toDouble, toBigInt and toInt

## 0.1.0
- Better parser and toString implementations
- Fix problems with rounding
Expand Down
5 changes: 5 additions & 0 deletions lib/src/big_decimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ class BigDecimal implements Comparable<BigDecimal> {
throw Exception('Invalid operation: Exponent should be between 0 and 999999999');
}

double toDouble() => intVal.toDouble() / BigInt.from(10).pow(scale).toDouble();
BigInt toBigInt({RoundingMode roundingMode = RoundingMode.UNNECESSARY}) =>
withScale(0, roundingMode: roundingMode).intVal;
int toInt({RoundingMode roundingMode = RoundingMode.UNNECESSARY}) => toBigInt(roundingMode: roundingMode).toInt();

BigDecimal withScale(
int newScale, {
RoundingMode roundingMode = RoundingMode.UNNECESSARY,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: big_decimal
version: 0.1.0
version: 0.2.0
description: >
A bugless implementation of BigDecimal in Dart based on Java's BigDecimal
Expand Down
66 changes: 66 additions & 0 deletions test/big_decimal_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,72 @@ void main() {
});
});

group(
'toDouble',
tabular((BigDecimal bd, double d) {
expect(bd.toDouble(), d);
}, [
tabCase(['1.5'.dec, 1.5]),
tabCase(['0'.dec, 0.0]),
tabCase(['-0'.dec, 0.0]),
tabCase([BigInt.from(9223372036854775807).dec, 9223372036854775807.0]),
// the above case and the one below are actually the same, because
// 9223372036854775807 is not perfectly representable by a double
// it gets "rounded" to 9223372036854776000.0
tabCase([BigInt.from(9223372036854775807).dec, 9223372036854776000.0]),
tabCase([BigInt.from(-9223372036854775808).dec, -9223372036854775808.0]),
tabCase([BigInt.from(-9223372036854775808).dec, -9223372036854776000.0]),
]),
);

group(
'toBigInt',
tabular((BigDecimal bd, BigInt bint, [RoundingMode roundingMode = RoundingMode.UNNECESSARY]) {
expect(bd.toBigInt(roundingMode: roundingMode), bint);
}, [
tabCase(['1.5'.dec, BigInt.from(1), RoundingMode.DOWN]),
tabCase(['1.5'.dec, BigInt.from(1), RoundingMode.FLOOR]),
tabCase(['1.5'.dec, BigInt.from(2), RoundingMode.UP]),
tabCase(['1.5'.dec, BigInt.from(2), RoundingMode.CEILING]),
tabCase(['1.5'.dec, BigInt.from(2), RoundingMode.HALF_EVEN]),
tabCase(['1.5'.dec, BigInt.from(2), RoundingMode.HALF_UP]),
tabCase(['1.5'.dec, BigInt.from(1), RoundingMode.HALF_DOWN]),
tabCase(['-1.5'.dec, BigInt.from(-1), RoundingMode.DOWN]),
tabCase(['-1.5'.dec, BigInt.from(-2), RoundingMode.FLOOR]),
tabCase(['-1.5'.dec, BigInt.from(-2), RoundingMode.UP]),
tabCase(['-1.5'.dec, BigInt.from(-1), RoundingMode.CEILING]),
tabCase(['-1.5'.dec, BigInt.from(-2), RoundingMode.HALF_EVEN]),
tabCase(['-1.5'.dec, BigInt.from(-2), RoundingMode.HALF_UP]),
tabCase(['-1.5'.dec, BigInt.from(-1), RoundingMode.HALF_DOWN]),
tabCase(['92233720368547758089999'.dec, BigInt.parse('92233720368547758089999')], 'very large integer'),
tabCase(['-92233720368547758089999'.dec, BigInt.parse('-92233720368547758089999')], 'very small integer'),
]),
);

group(
'toInt',
tabular((BigDecimal bd, int i, [RoundingMode roundingMode = RoundingMode.UNNECESSARY]) {
expect(bd.toInt(roundingMode: roundingMode), i);
}, [
tabCase(['1.5'.dec, 1, RoundingMode.DOWN]),
tabCase(['1.5'.dec, 1, RoundingMode.FLOOR]),
tabCase(['1.5'.dec, 2, RoundingMode.UP]),
tabCase(['1.5'.dec, 2, RoundingMode.CEILING]),
tabCase(['1.5'.dec, 2, RoundingMode.HALF_EVEN]),
tabCase(['1.5'.dec, 2, RoundingMode.HALF_UP]),
tabCase(['1.5'.dec, 1, RoundingMode.HALF_DOWN]),
tabCase(['-1.5'.dec, -1, RoundingMode.DOWN]),
tabCase(['-1.5'.dec, -2, RoundingMode.FLOOR]),
tabCase(['-1.5'.dec, -2, RoundingMode.UP]),
tabCase(['-1.5'.dec, -1, RoundingMode.CEILING]),
tabCase(['-1.5'.dec, -2, RoundingMode.HALF_EVEN]),
tabCase(['-1.5'.dec, -2, RoundingMode.HALF_UP]),
tabCase(['-1.5'.dec, -1, RoundingMode.HALF_DOWN]),
tabCase(['92233720368547758089999'.dec, 9223372036854775807], 'very large integer'),
tabCase(['-92233720368547758089999'.dec, -9223372036854775808], 'very small integer'),
]),
);

group(
'toString()',
tabular((Object a, String result) {
Expand Down

0 comments on commit 088ebf6

Please sign in to comment.