formatFixed method Null safety

  1. @protected
String? formatFixed(
  1. Value v,
  2. int fractionDigits
)

Implementation

@protected
String? formatFixed(Value v, int fractionDigits) {
  assert(fractionDigits >= 0 && fractionDigits < 11);
  // 10 is possible, when this is used for ->H.MS
  int exp = v.exponent;
  // First, try assuming no carry
  int mantissaDigits = min(10, exp + fractionDigits + 1);
  if (mantissaDigits < 0) {
    return null;
  }
  String mantissa = formatMantissaU(v, mantissaDigits);
  if (mantissa.length > mantissaDigits) {
    // If we got a carry,
    // it's like our exponent is one higher.
    exp++;
  } else if (mantissa.isEmpty) {
    return null;
  }
  fractionDigits = mantissa.length - exp - 1;
  if (fractionDigits < 0 || fractionDigits > 10) {
    return null;
  }
  int i = mantissa.length - fractionDigits;
  if (i <= 0) {
    mantissa = '${''.padLeft(1 - i, '0')}$mantissa';
    i = 1;
  }
  String minus = v.mantissaDigit(-1) == 9 ? '-' : '';
  final sp = '         '.substring(min(9, mantissa.length - 1));
  // If eleven total digits (like 0.0100000000), don't blow up
  return '$minus${mantissa.substring(0, i)}.${mantissa.substring(i)}$sp';
}