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

NumericUpDown Parses Decimal Value Incorrectly #3217

Closed
dkhughes opened this issue Apr 17, 2018 · 1 comment · Fixed by #3268
Closed

NumericUpDown Parses Decimal Value Incorrectly #3217

dkhughes opened this issue Apr 17, 2018 · 1 comment · Fixed by #3268
Assignees
Labels
Milestone

Comments

@dkhughes
Copy link

The NumericUpDown control is improperly parsing a decimal value that is entered with no leading zero in the en-US culture. For example, .1 becomes 1.0, 0.105 becomes 105, etc.

What steps will reproduce this issue?

This is a really easy issue to reproduce. An entire working example showing the failure is:

    class Program
    {
        // The regex copy pasted out of numericupdown control file...
        private static readonly Regex RegexStringFormatNumber = new Regex(@"[-+]?(?<![0-9][.,])\b[0-9]+(?:[.,\s][0-9]+)*[.,]?[0-9]?(?:[eE][-+]?[0-9]+)?\b(?!\.[0-9])", RegexOptions.Compiled);

        static void Main(string[] args)
        {
            // returns 1
            string leadingdec = GetAnyNumberFromText(".1");
            
            // returns 0.1
            string leadingZero = GetAnyNumberFromText("0.1");

            // returns 0.1 in double
            double.TryParse(".1", out double leadingDecDouble);

            // also returns 0.1 in double
            double.TryParse("0.1", out double leadingZeroDouble);
        }

        // The GetAnyNumberFromText function copy pasted out of numericupdown control file...
        private static string GetAnyNumberFromText(string text)
        {
            var matches = RegexStringFormatNumber.Matches(text);
            if (matches.Count > 0)
            {
                return matches[0].Value;
            }
            return text;
        }
    }

Expected outcome

We expect the parsed value to match the value that double.Parse would return on the raw string. For this example, ".1" should parse to 0.1 not 1.0 which it does currently.

Environment

  • MahApps.Metro: Latest master has issue
  • Windows OS 10
  • Visual Studio 2017
  • .NET Framework 4.5
@dkhughes
Copy link
Author

Altering the regexp to the following allows a leading decimal, but it may be too lenient for you:

@"[-+]?[0-9]?[.,]?\b[0-9]+(?:[.,\s][0-9]+)*[.,]?[0-9]?(?:[eE][-+]?[0-9]+)?\b(?!\.[0-9])"

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

Successfully merging a pull request may close this issue.

2 participants