diff --git a/lib/decimal.dart b/lib/decimal.dart index 6a3ccbb..a81424f 100644 --- a/lib/decimal.dart +++ b/lib/decimal.dart @@ -284,9 +284,17 @@ class Decimal implements Comparable { 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 [ if (negative) '-', - value.round(scale: fractionDigits).toStringAsFixed(fractionDigits), + value.toStringAsFixed(fractionDigits), 'e', if (eValue >= 0) '+', '$eValue', diff --git a/test/decimal_test.dart b/test/decimal_test.dart index b5f59d4..170886f 100644 --- a/test/decimal_test.dart +++ b/test/decimal_test.dart @@ -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');