formatScientific method Null safety

  1. @protected
String formatScientific(
  1. Value v,
  2. int digits
)

Format in scientific format, as modified by the (potentially overridden) constrainExponent function.

Implementation

@protected
String formatScientific(Value v, int digits) {
  int exp = v.exponent;
  String m = formatMantissaU(v, digits);
  String minus = v.mantissaDigit(-1) == 9 ? '-' : '';
  if (m.length > digits) {
    exp++;
    if (exp == 100) {
      if (digits == 7) {
        return '${minus}9.999999E+99';
      } else {
        return formatScientific(v, 7);
      }
    }
    m = formatMantissaU(v, digits - 1);
  }
  int shownExp = constrainExponent(exp);
  final int dpOffset = exp - shownExp;
  if (digits < dpOffset + 1) {
    m = m + '00'.substring(0, dpOffset + 1 - digits);
    digits = dpOffset + 1;
  }

  final sp = '       '.substring(min(digits, 7));
  final eSign = shownExp >= 0 ? '+' : '-';
  shownExp = shownExp.abs();
  return '$minus${m.substring(0, 1 + dpOffset)}.${m.substring(dpOffset + 1)}'
      '${sp}E$eSign${shownExp ~/ 10}${shownExp % 10}';
}