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

Locale aware double input #469

Open
t-unit opened this issue Oct 24, 2024 · 1 comment
Open

Locale aware double input #469

t-unit opened this issue Oct 24, 2024 · 1 comment
Labels
question Further information is requested

Comments

@t-unit
Copy link

t-unit commented Oct 24, 2024

Environment

Package version: 17.0.1

Describe your question

Depending on the local/language setting of an app, users expect to input floating point number with varying decimal separators. (E.g. see https://en.wikipedia.org/wiki/Decimal_separator#Conventions_worldwide).

I found that this library does not have any support for locale aware parsing, as DoubleValueAccessor is using double.tryParse that does not take any locale into account.

  1. What is the intended way to deal with different locales and floating point formats an this library?
  2. Shouldn't this library already take care of this an provide proper parsing for any locals? (It already has a dependency towards intl)

We came up with a custom value accessor:

class NumberFormatDoubleValueAccessor extends ControlValueAccessor<double, String> {
  NumberFormatDoubleValueAccessor(this.format);

  final NumberFormat format;

  @override
  String modelToViewValue(double? modelValue) => format.format(modelValue);

  @override
  double? viewToModelValue(String? viewValue) =>
      (viewValue == '' || viewValue == null)
          ? null
          : format.tryParse(viewValue)?.toDouble();
}

that allows specifying a format like NumberFormatDoubleValueAccessor(NumberFormat.decimalPattern('de')).

@t-unit t-unit added the question Further information is requested label Oct 24, 2024
@vasilich6107
Copy link
Contributor

What is the intended way to deal with different locales and floating point formats an this library

It depends on the individual project preferences

Shouldn't this library already take care of this an provide proper parsing for any locals?

Library provides a form engine that encapsulates the logic of manipulating with the form state + mechanisms of validators/asyncValidators
All basic valueAccessors are bonus points and serve nicely for kickstarting + as example of implementation.
Proper parsing of any locales is just one from tons of possible edge cases. The library should definitely avoid supporting all of them.
This library depends on intl package mostly due to the date time value accessor.
As time goes I'm pretty sure that this was not the best decision to include intl in scope of this package cause it just makes maintenance and update of the project more complex
Here is an example of PR that aimed to overcome consequences of having intl on board.
827c895

It would be better to move DateTimeValueAccessor reference implementation to README.md and make the life a bit easier.

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

No branches or pull requests

2 participants