toStringAsExponential method

String toStringAsExponential([
  1. int fractionDigits = 0
])

An exponential string-representation of this number with fractionDigits digits after the decimal point.

Implementation

String toStringAsExponential([int fractionDigits = 0]) {
  assert(fractionDigits >= 0);

  final negative = this < zero;
  var value = abs();
  var eValue = 0;
  while (value < one && value > zero) {
    value *= ten;
    eValue--;
  }
  while (value >= ten) {
    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.toStringAsFixed(fractionDigits),
    'e',
    if (eValue >= 0) '+',
    '$eValue',
  ].join();
}