d method

String d(
  1. int w, [
  2. int d = 0
])

Implementation

String d(int w, [int d = 0]) {
  if (isNaN) return 'NaN'.padLeft(w);
  if (isInfinite) {
    return switch ((isNegative, w)) {
      (true, >= 9) => '-Infinity',
      (true, >= 4) => '-Inf',
      (false, >= 8) => 'Infinity',
      (false, >= 3) => 'Inf',
      _ => '*' * w
    }
        .padLeft(w);
  }

  var [num, exponent] = toStringAsExponential(d - 1).split('e');
  num = num.replaceFirstMapped(RegExp('([+-]?)(\\d).'), (match) {
    return '${match[1]}0.${match[2]}';
  });
  final exp = int.parse(exponent) + (this == 0 ? 0 : 1);
  return '$num${exp.abs().toString().length <= 2 ? 'D' : ''}${exp < 0 ? '-' : '+'}${exp.abs().toString().padLeft(2, '0')}'
      .padLeft(w);
}