Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not allow entering 0 in the decimal section #9

Open
Sun3 opened this issue Aug 15, 2020 · 7 comments
Open

Does not allow entering 0 in the decimal section #9

Sun3 opened this issue Aug 15, 2020 · 7 comments

Comments

@Sun3
Copy link

Sun3 commented Aug 15, 2020

When using the ThousandsFormatter(allowFraction: true) and you try to enter a zero in the decimal section it does not allow it.

Does not allow the following zeros in the decimal section:

3,500.01
3,500.10

Any thoughts to make it work?
Thank you

@Sun3
Copy link
Author

Sun3 commented Aug 28, 2020

Checking to see if this is something that will be fixed, it's a major issue.

Thank you.

@Sun3
Copy link
Author

Sun3 commented Aug 31, 2020

I have narrowed down where it's causing the .0 or .10 not to show up but is removed when typing it:

When you enter a number 1,500.0 the double.tryParse(decimalDigits) removes the .0 as you are typing, making it impossible to enter decimals that start with a 0. This is common in the financial world like 1,500.0125.

Also when you enter a number 1,500.10 the double.tryParse(decimalDigits) removes the 0 after the .10 making it impossible to enter decimals that have zeros 0 after a digit in the decimal section. This is also common in the financial world like 1,500.1045.

Code that is causing the issue:

  @override
  String _formatPattern(String digits) {
    if (digits == null || digits.isEmpty) return digits;
    num number;
    if (allowFraction) {
      String decimalDigits = digits;
      if (_decimalSeparator != '.') {
        decimalDigits = digits.replaceFirst(RegExp(_decimalSeparator), '.');
      }
      // THIS CAUSES THE ISSUE
      number = double.tryParse(decimalDigits) ?? 0.0;
    } else {
      number = int.tryParse(digits) ?? 0;
    }
    final result = (formatter ?? _formatter).format(number);
    if (allowFraction && digits.endsWith(_decimalSeparator)) {
      return '$result$_decimalSeparator';
    }
    return result;
  }

Thank you and I hope this helps to solve this issue.

@Sun3
Copy link
Author

Sun3 commented Sep 14, 2020

Checking to see if there is any news on this issue. I am wondering if this package is not going to be updated.

Thank you.

@Sun3
Copy link
Author

Sun3 commented Sep 15, 2020

If anyone is interested in a quick fix I have placed my solution:

// Fix the .0 or .01 or .10 and similar issues
if (digits.contains('.')) {
  List<String> decimalPlacesValue = digits.split(".");
  String decimalOnly = decimalPlacesValue[1];
  double digitsOnly = double.tryParse(decimalPlacesValue[0]);
  String result = (formatter ?? _formatter).format(digitsOnly);
  result = result + '.' + '$decimalOnly';
  return result;
}

The full code for the ThousandsFormatter class:

class ThousandsFormatter extends NumberInputFormatter {
  static final NumberFormat _formatter = NumberFormat.decimalPattern();

  final FilteringTextInputFormatter _decimalFormatter;
  final String _decimalSeparator;
  final RegExp _decimalRegex;

  final NumberFormat formatter;
  final bool allowFraction;

  ThousandsFormatter({this.formatter, this.allowFraction = false})
      : _decimalSeparator = (formatter ?? _formatter).symbols.DECIMAL_SEP,
        _decimalRegex = RegExp(allowFraction
            ? '[0-9]+([${(formatter ?? _formatter).symbols.DECIMAL_SEP}])?'
            : r'\d+'),
        _decimalFormatter = FilteringTextInputFormatter.allow(RegExp(allowFraction
            ? '[0-9]+([${(formatter ?? _formatter).symbols.DECIMAL_SEP}])?'
            : r'\d+'));

  @override
  String _formatPattern(String digits) {
    if (digits == null || digits.isEmpty) return digits;
    num number;
    if (allowFraction) {
      String decimalDigits = digits;
      if (_decimalSeparator != '.') {
        decimalDigits = digits.replaceFirst(RegExp(_decimalSeparator), '.');
      }
      number = double.tryParse(decimalDigits) ?? 0.0;
    } else {
      number = int.tryParse(digits) ?? 0;
    }
    final result = (formatter ?? _formatter).format(number);
    if (allowFraction && digits.endsWith(_decimalSeparator)) {
      return '$result$_decimalSeparator';
    }

    // Fix the .0 or .01 or .10 and similar issues
    if (digits.contains('.')) {
      List<String> decimalPlacesValue = digits.split(".");
      String decimalOnly = decimalPlacesValue[1];
      double digitsOnly = double.tryParse(decimalPlacesValue[0]);
      String result = (formatter ?? _formatter).format(digitsOnly);
      result = result + '.' + '$decimalOnly';
      return result;
    }
    return result;
  }

  @override
  TextEditingValue _formatValue(
      TextEditingValue oldValue, TextEditingValue newValue) {
    return _decimalFormatter.formatEditUpdate(oldValue, newValue);
  }

  @override
  bool _isUserInput(String s) {
    return s == _decimalSeparator || _decimalRegex.firstMatch(s) != null;
  }
}

I hope this helps anyone else having the same issue.

@chitgoks
Copy link

i hope the author will update this. seems to be ignored

@shabeenabarde
Copy link

If anyone is interested in a quick fix I have placed my solution:

// Fix the .0 or .01 or .10 and similar issues
if (digits.contains('.')) {
  List<String> decimalPlacesValue = digits.split(".");
  String decimalOnly = decimalPlacesValue[1];
  double digitsOnly = double.tryParse(decimalPlacesValue[0]);
  String result = (formatter ?? _formatter).format(digitsOnly);
  result = result + '.' + '$decimalOnly';
  return result;
}

The full code for the ThousandsFormatter class:

class ThousandsFormatter extends NumberInputFormatter {
  static final NumberFormat _formatter = NumberFormat.decimalPattern();

  final FilteringTextInputFormatter _decimalFormatter;
  final String _decimalSeparator;
  final RegExp _decimalRegex;

  final NumberFormat formatter;
  final bool allowFraction;

  ThousandsFormatter({this.formatter, this.allowFraction = false})
      : _decimalSeparator = (formatter ?? _formatter).symbols.DECIMAL_SEP,
        _decimalRegex = RegExp(allowFraction
            ? '[0-9]+([${(formatter ?? _formatter).symbols.DECIMAL_SEP}])?'
            : r'\d+'),
        _decimalFormatter = FilteringTextInputFormatter.allow(RegExp(allowFraction
            ? '[0-9]+([${(formatter ?? _formatter).symbols.DECIMAL_SEP}])?'
            : r'\d+'));

  @override
  String _formatPattern(String digits) {
    if (digits == null || digits.isEmpty) return digits;
    num number;
    if (allowFraction) {
      String decimalDigits = digits;
      if (_decimalSeparator != '.') {
        decimalDigits = digits.replaceFirst(RegExp(_decimalSeparator), '.');
      }
      number = double.tryParse(decimalDigits) ?? 0.0;
    } else {
      number = int.tryParse(digits) ?? 0;
    }
    final result = (formatter ?? _formatter).format(number);
    if (allowFraction && digits.endsWith(_decimalSeparator)) {
      return '$result$_decimalSeparator';
    }

    // Fix the .0 or .01 or .10 and similar issues
    if (digits.contains('.')) {
      List<String> decimalPlacesValue = digits.split(".");
      String decimalOnly = decimalPlacesValue[1];
      double digitsOnly = double.tryParse(decimalPlacesValue[0]);
      String result = (formatter ?? _formatter).format(digitsOnly);
      result = result + '.' + '$decimalOnly';
      return result;
    }
    return result;
  }

  @override
  TextEditingValue _formatValue(
      TextEditingValue oldValue, TextEditingValue newValue) {
    return _decimalFormatter.formatEditUpdate(oldValue, newValue);
  }

  @override
  bool _isUserInput(String s) {
    return s == _decimalSeparator || _decimalRegex.firstMatch(s) != null;
  }
}

I hope this helps anyone else having the same issue.

Rahhh!! Thank you so much for this fix!

@FranLMSP
Copy link

@Sun3 Thank you! I was having the same issue, that fixed it. Hopefully it gets fixed on the main repo but the issue seems to be ignored :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants