FloatFormatter constructor

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

Implementation

FloatFormatter(this._arg, var fmt_type, var options)
    : super(fmt_type, options) {
  _arg = _arg.toDouble();

  if (_arg < 0) {
    _is_negative = true;
    _arg = -_arg;
  }

  var arg_str =
  _arg == _arg.truncate() ? _arg.toStringAsFixed(1) : _arg.toString();

  var m1 = _number_rx.firstMatch(arg_str);
  if (m1 != null) {
    var int_part = m1.group(1);
    var 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 = int_part!.length;
    _digits.addAll(int_part.split('').map(int.parse));
    _digits.addAll(fraction!.split('').map(int.parse));

    if (int_part.length == 1) {
      if (int_part == '0') {
        var leading_zeroes_match = _leading_zeroes_rx.firstMatch(fraction);

        if (leading_zeroes_match != null) {
          var zeroes_count = leading_zeroes_match.group(1)!.length;
          // print("zeroes_count=${zeroes_count}");
          _exponent =
          zeroes_count > 0 ? -(zeroes_count + 1) : zeroes_count - 1;
        } else {
          _exponent = 0;
        }
      } // else int_part != 0
      else {
        _exponent = 0;
      }
    } else {
      _exponent = int_part.length - 1;
    }
  } else {
    var m2 = _expo_rx.firstMatch(arg_str);
    if (m2 != null) {
      var int_part = m2.group(1);
      var fraction = m2.group(2);
      _exponent = int.parse(m2.group(3)!);

      if (_exponent > 0) {
        var diff = _exponent - fraction!.length + 1;
        _decimal = _exponent + 1;
        _digits.addAll(int_part!.split('').map(int.parse));
        _digits.addAll(fraction.split('').map(int.parse));
        _digits.addAll(
            Formatter.get_padding(diff, '0').split('').map(int.parse));
      } else {
        var diff = int_part!.length - _exponent - 1;
        _decimal = int_part.length;
        _digits.addAll(
            Formatter.get_padding(diff, '0').split('').map(int.parse));
        _digits.addAll(int_part.split('').map(int.parse));
        _digits.addAll(fraction!.split('').map(int.parse));
      }
    } // else something wrong
  }
  _has_init = true;
  //print("arg_str=${arg_str}");
  //print("decimal=${_decimal}, exp=${_exponent}, digits=${_digits}");
}