Skip to content

Conversation

stephenquan
Copy link
Contributor

@stephenquan stephenquan commented Jun 16, 2025

Description of Change

Parse a double using InvariantCulture to avoid issues when CurrentCulture changes, such as to 'ar-AR'.

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (omitted, since this is a minor change)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated: (no change)

Additional information

None

@@ -362,7 +362,7 @@ bool ParsePrimary()
if (ParsePattern(EvaluateNumberPattern()))
{
string _number = PatternMatch.Groups[1].Value;
RPN.Add(new MathToken(MathTokenType.Value, _number, double.Parse(_number)));
RPN.Add(new MathToken(MathTokenType.Value, _number, double.Parse(_number, System.Globalization.CultureInfo.InvariantCulture)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each converter provides CultureInfo. Maybe it's better to pass it here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevents the ability to use a comma as a decimal separator too.

Perhaps we can make it controllable via a property on the converter instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ParsePattern(EvaluateNumberPattern()) has a regex in it that pretty much assumes US english. The double.Parse() is merely matching it.

If we change it so that it reacts to a passed-in CultureInfo either via the converter (or equivalent), then, I would need to supply a regex for it, else rework how the numbers are being extracted from the string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VladislavAntonyuk I tried passing a custom CultureInfo to the converter via XAML but couldn’t find the syntax. It seems the converter uses CultureInfo.CurrentUICulture, which brings us back to the issue, i.e. expressions can fail in certain cultures.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VladislavAntonyuk @bijington InvariantCulture appears to be used consistently for numeric values in XAML and for numeric expressions in C#. Based on that, I still believe InvariantCulture is the appropriate choice for MathExpressionConverter. After further research, CurrentCulture is generally reserved for UI-facing operations, which doesn't apply to the kind of expressions handled by this converter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. That's fine with me 👍

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean that I can't use comma as a separator?

Copy link
Contributor Author

@stephenquan stephenquan Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VladislavAntonyuk, the analogy would be to compare it with another double precision XAML property, e.g. VisualElement.Opacity. When one sets that in XAML, don't they also specify that double using InvariantCulture? e.g.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VladislavAntonyuk, are you okay with this explanation? I will assume this discussion is resolved.

@@ -362,7 +362,7 @@ bool ParsePrimary()
if (ParsePattern(EvaluateNumberPattern()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to turn on the CA1305 analyzer in the project to avoid similar bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartyIX based on your feedback, I applied the CA1305 analyzer and found multiple conversions in MathExpression.shared.cs that needed updates to satisfy the analyzer. After the changes all unit tests for the math converters still passed.

The CA1305 analyzer also picked up new issues which isn't part of the scope of this PR. Would you like me to create split 3 issues to look? (Camera CA1305, SpeechToText CA1305, Badge CA1305)?

F:\Maui\src\CommunityToolkit.Maui.Camera\CameraInfo.shared.cs(99,4): error CA1305: The behavior of 'StringBuilder.Append(ref StringBuilder.AppendInterpolatedStringHandler)' could vary based on the current user's locale settings. Replace this call in 'CameraInfo.ToString()' with a call to 'StringBuilder.Append(IFormatProvider, ref StringBuilder.AppendInterpolatedStringHandler)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Camera\CameraInfo.shared.cs(99,4): error CA1305: The behavior of 'StringBuilder.Append(ref StringBuilder.AppendInterpolatedStringHandler)' could vary based on the current user's locale settings. Replace this call in 'CameraInfo.ToString()' with a call to 'StringBuilder.Append(IFormatProvider, ref StringBuilder.AppendInterpolatedStringHandler)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Camera\CameraInfo.shared.cs(99,4): error CA1305: The behavior of 'StringBuilder.Append(ref StringBuilder.AppendInterpolatedStringHandler)' could vary based on the current user's locale settings. Replace this call in 'CameraInfo.ToString()' with a call to 'StringBuilder.Append(IFormatProvider, ref StringBuilder.AppendInterpolatedStringHandler)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Camera\CameraInfo.shared.cs(99,4): error CA1305: The behavior of 'StringBuilder.Append(ref StringBuilder.AppendInterpolatedStringHandler)' could vary based on the current user's locale settings. Replace this call in 'CameraInfo.ToString()' with a call to 'StringBuilder.Append(IFormatProvider, ref StringBuilder.AppendInterpolatedStringHandler)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Camera\CameraInfo.shared.cs(99,4): error CA1305: The behavior of 'StringBuilder.Append(ref StringBuilder.AppendInterpolatedStringHandler)' could vary based on the current user's locale settings. Replace this call in 'CameraInfo.ToString()' with a call to 'StringBuilder.Append(IFormatProvider, ref StringBuilder.AppendInterpolatedStringHandler)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Core\Essentials\SpeechToText\OfflineSpeechToTextImplementation.android.cs(188,56): error CA1305: The behavior of 'int.ToString()' could vary based on the current user's locale settings. Replace this call in 'RecognitionSupportCallback.OnError(int)' with a call to 'int.ToString(IFormatProvider)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Core\Essentials\Badge\BadgeImplementation.windows.cs(22,40): error CA1305: The behavior of 'uint.ToString()' could vary based on the current user's locale settings. Replace this call in 'BadgeImplementation.SetCount(uint)' with a call to 'uint.ToString(IFormatProvider)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Build failed with 7 error(s) and 81 warning(s) in 3.8s

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm in no position to say what you should do or not, I'm just a random passerby :)) However, I believe it would be a nice new PR or PRs1 to avoid cramming this PR.

Footnotes

  1. If the number of necessary changes is small, then probably just one PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartyIX, as per your feedback, I have created a new split issue #2744 to avoid cramming this PR.

Copy link
Collaborator

@VladislavAntonyuk VladislavAntonyuk Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can change the pattern to something like this:

@"^-?\d+[.,]?\d*$"

It should match:

  • 123
  • -123.45
  • -123,45

@dotnet-policy-service dotnet-policy-service bot added stale The author has not responded in over 30 days help wanted This proposal has been approved and is ready to be implemented labels Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted This proposal has been approved and is ready to be implemented stale The author has not responded in over 30 days
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] MathExpressionConverter doesn't support negative numbers when in Arabic (ar_AR)
5 participants