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

iOS Numeric Format setting not respected #110430

Open
LiamMorrow opened this issue Dec 4, 2024 · 9 comments
Open

iOS Numeric Format setting not respected #110430

LiamMorrow opened this issue Dec 4, 2024 · 9 comments
Assignees
Milestone

Comments

@LiamMorrow
Copy link

LiamMorrow commented Dec 4, 2024

Description

When using decimal.Parse using the default CultureInfo (or not specifying one), MAUI does not respect the user's decimal separator options in iOS. Instead, it simply uses the language's default. What this means is that when a user is prompted for numerical, decimal input, the iOS keyboard presents them with a comma separator (as they have configured), but decimal.Parse treats it as a grouping separator.

Note that if the user specifies a LANGUAGE which uses commas as a decimal separator, everything works as expected and decimal parse treats commas as decimal separators.

Screenshot showing how the default culture parses a decimal, and the correct parsing

Image

Screenshot showing that the keyboard only allows entering decimals with commas

Image

Code driving the parsing logic
 if (decimal.TryParse(e.NewTextValue, out decimal result))
        {
            resultLabel.Text = $"Parsed value with default culture: {result}";
        }
        else
        {
            resultLabel.Text = "Invalid input";
        }
#if IOS
        var correctedCulture = CultureInfo.CurrentCulture.Clone() as CultureInfo;
        correctedCulture.NumberFormat.NumberDecimalSeparator = NSLocale
            .CurrentLocale
            .DecimalSeparator;
        correctedCulture.NumberFormat.NumberGroupSeparator = NSLocale
            .CurrentLocale
            .GroupingSeparator;

        if (decimal.TryParse(e.NewTextValue, correctedCulture, out decimal result2))
        {
            resultLabel2.Text = $"Parsed value with culture from IOS settings: {result2}";
        }
        else
        {
            resultLabel2.Text = "Invalid input";
        }
#endif
Video showing off behaviour
Screen.Recording.2024-12-04.at.12.14.58.mov

Steps to Reproduce

  1. Open iOS settings -> General -> Language
  2. Set language to English
  3. Set Number Format to 1.234.234,83 (the one which uses comma as a decimal separator)
  4. In a Maui app type in a numeric editor (notice iOS presents only a comma as the decimal separator)
  5. Notice that decimal.Parse treats the comma as a grouping separator

An example repo for parsing can be found here:
https://github.com/LiamMorrow/maui-decimal-repro

Link to public reproduction project repository

https://github.com/LiamMorrow/maui-decimal-repro

Version with bug

9.0.12 SR1.2

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 18

Did you find any workaround?

Cloning the current culture, then using the proper separators in the clone, and setting it as the current culture on app startup does fix this. See this PR for an example.

Relevant log output

@LiamMorrow LiamMorrow changed the title iOS Numeric Format not Respected iOS Numeric Format setting not respected Dec 4, 2024
@jfversluis

This comment has been minimized.

@dotnet dotnet locked and limited conversation to collaborators Dec 4, 2024
@rolfbjarne
Copy link
Member

This sounds like the default CultureInfo isn't instantiated correctly, which is an issue for dotnet/runtime.

@rolfbjarne rolfbjarne reopened this Dec 5, 2024
@dotnet dotnet unlocked this conversation Dec 5, 2024
@mattleibow mattleibow transferred this issue from dotnet/maui Dec 5, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 5, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-globalization
See info in area-owners.md if you want to be subscribed.

@tarekgh tarekgh added the os-ios Apple iOS label Dec 5, 2024
Copy link
Contributor

Tagging subscribers to 'os-ios': @vitek-karas, @kotlarmilos, @ivanpovazan, @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

@tarekgh
Copy link
Member

tarekgh commented Dec 5, 2024

@matouskozak could you please have a look and triage?

@LiamMorrow
Copy link
Author

LiamMorrow commented Dec 5, 2024

For the record, this was originally reported here:
LiamMorrow/LiftLog#332

Note that the user seems to think it is due to the mix of Swedish&English keyboard, introduced in ios18

@matouskozak matouskozak removed the untriaged New issue has not been triaged by the area owner label Dec 9, 2024
@matouskozak matouskozak self-assigned this Dec 9, 2024
@matouskozak matouskozak added this to the 10.0.0 milestone Dec 9, 2024
@matouskozak
Copy link
Member

matouskozak commented Dec 9, 2024

@LiamMorrow Thanks a lot for the great issue description with concrete examples. I'm able to reproduce the issue, on both .net 8 and .net 9 MAUI apps.

Do you with which version of .NET it used used to work correctly?

Anyways, I'll investigate why the CurrentCulture is not set properly with the iOS number format setting.

@LiamMorrow
Copy link
Author

Thanks @matouskozak for looking into it!

Do you know when was the last time this used to work correctly?

I have a feeling it has never worked correctly, however iOS 18 has released new keyboards which allow for multi locale keyboards which has probably made it much easier for it to happen. I don't think many users would stray from their default locale's number format otherwise. The user who reported it to me says it started occuring when they switched to the Swedish/English keyboard.

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

No branches or pull requests

6 participants