g method

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

Implementation

String g(int w, [int d = 0]) {
  if (isNaN) return this.d(w - 4, d) + ' ' * 4;
  var n = abs();
  const minFp = 0.1;
  final maxFp = pow(10, d);
  if (n != 0) {
    if (n < minFp || n >= maxFp) return e(w, d);
    var prevFp = minFp;
    var nextFp = prevFp * 10;
    if (n >= 10) d -= 1;
    while (d > 0 &&
        !(n >= prevFp && (n < 10 || n == maxFp ? n < nextFp : n <= nextFp))) {
      prevFp = nextFp;
      nextFp *= 10;
      d -= 1;
    }
  } else {
    d -= 1;
  }

  if (d == 0) {
    return '${truncate().toString()}.'.padLeft(w - 4) + ' ' * 4;
  }
  return f(w - 4, d) + ' ' * 4;
}