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

double.TryParse of "0,12" incorrectly returns true with result of 0.12. #25868

Open
Tracked by #47244
DonaldRich opened this issue Apr 12, 2018 · 8 comments
Open
Tracked by #47244

Comments

@DonaldRich
Copy link

When executing the following the return value is true (parsing succeeded) and the number value becomes 0.12:

double.TryParse("0,12", NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out number)

The input is not a valid US number since the comma proceeded by a zero and followed by only 2 digits. US numbers with a comma are only valid if the integer part is a 1 to 3 digits (but not a sequence of zero digits) followed by zero or more groups of 3 digits separated by commas.

VisualStudio 2015 Version 14.0.25431.01 Update 3
Microsoft .NET Framework
Version 4.7.02556
Build for x64 and .Net 4.5

Installed Version: Community

@ghost
Copy link

ghost commented Apr 13, 2018

using System;
using System.Globalization;
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(double.TryParse("0,12", NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out double number));
        Console.WriteLine(number);
    }
}

Prints:

True
12

with net40, net45, net452, net46, net461, net47, netcoreapp2.0 and netcoreapp2.1.

How is it producing 0.12 on your system?

@nil4
Copy link
Contributor

nil4 commented Apr 13, 2018

@DonaldRich do you see different results if you change CultureInfo.GetCultureInfo("en-US") to new CultureInfo("en-US", useUserOverride: false)?

@DonaldRich
Copy link
Author

DonaldRich commented Apr 13, 2018 via email

@ghost
Copy link

ghost commented Apr 13, 2018

double.Parse with NumberStyles.Number will parse out the numbers from string, if string has no alphabet. Use NumberStyles.Float to make it fail.

@DonaldRich
Copy link
Author

DonaldRich commented Apr 13, 2018 via email

@ghost
Copy link

ghost commented Apr 15, 2018

Looking at the code, https://github.com/dotnet/corefx/blob/27149e4/src/Common/src/CoreLib/System/Number.Parsing.cs#L399, it seems like it doesn't account for position of thousand separator (except when , appears at position 0 it returns false), but only its presence. Even double.TryParse("333,333,22,22,1,1,1,1,,,,,,,", NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out double number) is returning true.
@mazong1123, @stephentoub, the behavior is consistent with .NET Framework, but undesired. Can we make a breaking change in this area?

@mazong1123
Copy link
Contributor

Sounds like a bug. @stephentoub @jkotas do we have any historical reason for missing this validation?

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@tannergooding tannergooding added area-System.Numerics and removed area-System.Runtime untriaged New issue has not been triaged by the area owner labels Jun 23, 2020
@ghost
Copy link

ghost commented Jun 23, 2020

Tagging subscribers to this area: @tannergooding
Notify danmosemsft if you want to be subscribed.

@tannergooding tannergooding added the needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration label Jan 15, 2021
@jeffhandley jeffhandley removed the needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration label Nov 10, 2024
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

7 participants