0

In my .NET MAUI App, I have Entrys that should allow positive double values. Also, I allow to switch Languages. Currently, I only support English and German.

Anyway, in English Culture Settings, „.“ is the decimal separator and in German it would be „,“.

In my Pixel 5 - API 33 (Android 13.0 - API 33) Emulator, I only get „.“ when I hit „,“ in German Language Setting and also when I hit it in English Language Settings.

Unfortunately, it is interpreted differently then and actually an input of „84.2“ is then stored as 842“.

Is there an easy way to deal with that, so that the values are stored and loaded correctly independent from the current Culture?

Also, is there an easy way to allow just positive values and also be able to have „0“ and „0.“ while typing, but not „.456“?

Or maybe some of you know a much easier way than parsing a certain Regex during TextChanged (what I currently do) to allow positive double values culture independent? Also want to store the values in SQLite then.

=========== Code of Entry TextChanged ======

private void EntryControl_TextChanged(object sender, TextChangedEventArgs e)
{
    var entry sender as Entry;
    var input entry.Text;
    var match = Regex.Match(input, „^(0|0[.,][0-9]*|[1-9]+[.,]*[0-9]*)$“);

    if (input.Length == 0)
    {
        return;
    }

    if (!match.Success)
    {
        entry.Text = e.OldTextValue;
    }

    entry.CursorPosition = e.NewTextValue.Length;
}
OXO
  • 391
  • 1
  • 8
  • 1
    Please post the relevant code. .NET provides a variety of Parse() functions for numeric types that account for different locales. And the internal storage of numeric types is independent of the locale specific way they are shown to the user – Jason Apr 21 '23 at 07:19
  • I added the Code how I parser the allowed Input for positive floating-points. Better way to do this? The Binding of the Entry Text in XAML is to a BindableProperty of type string and not to a double as I thought this might cause less problems in the end. Problem for me is, regardless of what I type “,” or “.” in my Emulator, I get a “.” in my Entry. Under the hood it is like the “.” Input is interpreted differently in the end. Info: Keyboard=“Numeric” in my Entry – OXO Apr 21 '23 at 09:36
  • 1
    Usually I validate inputs. Let the user fill whatever he wants. Parse it right. And handle invalid inputs when he is finished. One time he see the red Entry, with text bellow "age must be positive", and next day he will learn not to enter "-3" or "0". Just pick the right keyboard and hint him. I haven't seen any complains. – H.A.H. Apr 21 '23 at 14:39
  • I got away from that a little bit reading comments here. It was more like, Hhm why not evaluate directly and show what’s wrong right away. But, also what you say is good for me, as I came with the classic Windows Forms background. Well, but in any case what is needed is the parsing and showing what is allowed with the correct culture, but just in the end instead of in TextChanged – OXO Apr 21 '23 at 18:07

1 Answers1

0

Is there an easy way to deal with that, so that the values are stored and loaded correctly independent from the current Culture?

First, you can use the Keyboard="Numeric to make the keyboard only show the number while typing. It is convinent for the people to choose the number.

<Entry Placeholder="Enter something here" TextChanged="Entry_TextChanged" Keyboard="Numeric"></Entry>

Second, you can use the Double.TryParse Method to parse the string representation of numbers that have a particular style and are formatted using the conventions of a particular culture. Here is the sample you can refer to.

Guangyu Bai - MSFT
  • 2,555
  • 1
  • 2
  • 8