Skip to content

Commit

Permalink
fix(#74): use normalized notation on toStringAsExponential (#73)
Browse files Browse the repository at this point in the history
* refactor: use normalized notation on toStringAsExponential

* remove unnecessary round

* clean up

Co-authored-by: Alexandre Ardhuin <alexandre.ardhuin@gmail.com>
  • Loading branch information
canastro and a14n authored Apr 19, 2022
1 parent 266a43a commit d4cc54c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/decimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,17 @@ class Decimal implements Comparable<Decimal> {
value = (value / ten).toDecimal();
eValue++;
}
value = value.round(scale: fractionDigits);
// If the rounded value is 10, then divide it once more to make it follow
// the normalized scientific notation. See https://github.com/a14n/dart-decimal/issues/74
if (value == ten) {
value = (value / ten).toDecimal();
eValue++;
}

return <String>[
if (negative) '-',
value.round(scale: fractionDigits).toStringAsFixed(fractionDigits),
value.toStringAsFixed(fractionDigits),
'e',
if (eValue >= 0) '+',
'$eValue',
Expand Down
2 changes: 2 additions & 0 deletions test/decimal_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ void main() {
// issue https://github.com/a14n/dart-decimal/issues/48
expectThat(dec('1.7976931348623157e+310').toStringAsExponential(10))
.equals('1.7976931349e+310');
// issue https://github.com/a14n/dart-decimal/issues/74
expectThat(dec('9.9999e+7').toStringAsExponential(2)).equals('1.00e+8');
});
test('toStringAsPrecision(int precision)', () {
expectThat(dec('0').toStringAsPrecision(1)).equals('0');
Expand Down

0 comments on commit d4cc54c

Please sign in to comment.