FloatFormatter constructor

FloatFormatter(
  1. double? _arg,
  2. dynamic formatType,
  3. dynamic options
)

Implementation

FloatFormatter(this._arg, var formatType, var options)
    : super(formatType, options) {
  if ((_arg ?? 0) < 0) {
    this._isNegative = true;
    _arg = -(_arg ?? 0);
  }
  String argStr = "${(_arg ?? 0)}";
  Match? m1 = _numberRx.firstMatch(argStr);
  if (m1 != null) {
    String intPart = m1.group(1) ?? "";
    String fraction = m1.group(2) ?? "";
    /*
     * Cases:
     * 1.2345    = 1.2345e0  -> [12345]    e+0 d1  l5
     * 123.45    = 1.2345e2  -> [12345]    e+2 d3  l5
     * 0.12345   = 1.2345e-1 -> [012345]   e-1 d1  l6
     * 0.0012345 = 1.2345e-3 -> [00012345] e-3 d1  l8
     */
    _decimal = intPart.length;
    _digits.addAll(intPart.split('').map(int.parse));
    _digits.addAll(fraction.split('').map(int.parse));
    if (intPart.length == 1) {
      if (intPart == '0') {
        Match? leadingZeroesMatch = _leadingZeroesRx.firstMatch(fraction);
        if (leadingZeroesMatch != null) {
          int zeroesCount = leadingZeroesMatch.group(1)?.length ?? 0;
          _exponent = zeroesCount > 0 ? -(zeroesCount + 1) : zeroesCount - 1;
        } else {
          _exponent = 0;
        }
      } // else int_part != 0
      else {
        _exponent = 0;
      }
    } else {
      _exponent = intPart.length - 1;
    }
  } else {
    Match? m2 = _expoRx.firstMatch(argStr);
    if (m2 != null) {
      String intPart = m2.group(1) ?? "";
      String fraction = m2.group(2) ?? "";
      _exponent = m2.group(3)!.toInt;

      if (_exponent > 0) {
        int diff = _exponent - fraction.length + 1;
        _decimal = _exponent + 1;
        _digits.addAll(intPart.split('').map(int.parse));
        _digits.addAll(fraction.split('').map(int.parse));
        _digits
            .addAll(Formatter.getPadding(diff, '0').split('').map(int.parse));
      } else {
        int diff = intPart.length - _exponent - 1;
        _decimal = intPart.length;
        _digits
            .addAll(Formatter.getPadding(diff, '0').split('').map(int.parse));
        _digits.addAll(intPart.split('').map(int.parse));
        _digits.addAll(fraction.split('').map(int.parse));
      }
    } // else something wrong
  }
  _hasInit = true;
}