Bug 34757 - Double.TryParse incorrectly returns true
Summary: Double.TryParse incorrectly returns true
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 4.0.0
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2015-10-11 22:53 UTC by Tristan
Modified: 2015-10-13 03:50 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Tristan 2015-10-11 22:53:47 UTC
double num;
return Double.TryParse("$5.0", out num);

This parses to 5.0 and incorrectly returns true. I have also tried the TryParse overload with an invariant culture and also US culture with no success.


=== Xamarin Studio ===

Version 5.9.5 (build 10)
Installation UUID: 4f969ef5-6bf3-4889-92eb-941d1e72a2c8
Runtime:
	Mono 4.0.3 ((detached/d6946b4)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400030020

=== Apple Developer Tools ===

Xcode 6.4 (7720)
Build 6E35b

=== Xamarin.iOS ===

Version: 8.10.4.46 (Business Edition)
Hash: 2c66d2f
Branch: master
Build date: 2015-08-04 13:52:25-0400

=== Xamarin.Android ===

Not Installed

=== Xamarin Android Player ===

Not Installed

=== Xamarin.Mac ===

Not Installed

=== Build Information ===

Release ID: 509050010
Git revision: 48d16bc4f12ce3938964fc7c3d72fdc6887ad4ad
Build date: 2015-08-18 16:55:24-04
Xamarin addins: c2d51b360ad9f59e689046d47030df27de28f94a

=== Operating System ===

Mac OS X 10.10.5
Darwin Bruces-Mac-mini.local 14.5.0 Darwin Kernel Version 14.5.0
    Wed Jul 29 02:26:53 PDT 2015
    root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64
Comment 1 Marek Safar 2015-10-12 11:18:17 UTC
Works for me as expected, True on US like locales False for others. My test code

using System;
using System.Globalization;

class X
{
	public static void Main ()
	{
		double num;
		var d = Double.TryParse("$5.0", NumberStyles.Any, CultureInfo.GetCultureInfo("en-us"), out num);
		Console.WriteLine (d);
		return;
	}
}
Comment 2 Tristan 2015-10-12 19:34:57 UTC
I have had success with the overload 

class X
{
    public static void Main ()
    {
        double num;
        var d = Double.TryParse("$5.0", 
                    System.Globalization.NumberStyles.Float |
                    System.Globalization.NumberStyles.AllowThousands, 
                    CultureInfo.CurrentCulture.NumberFormat, 
                    out num);
        Console.WriteLine (d);
        return;
    }
}

However the default Double.TryParse is incorrect as it allows a currency symbol

class X
{
    public static void Main ()
    {
        double num;
        var d = Double.TryParse("$5.0", out num);
        Console.WriteLine (d);
        return;
    }
}

According to https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx

"The s parameter is interpreted by using a combination of the NumberStyles.Float and NumberStyles.AllowThousands flags. This means that white space and thousands separators are allowed but currency symbols are not."
Comment 3 Marek Safar 2015-10-13 03:50:54 UTC
Following case is already fixed in Mono 4.2

using System;
using System.Threading;
using System.Globalization;

public class Wibble 
{
	public static void Main () {
        Double res;
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-us");
        var d = Double.TryParse("$5.0", out res);
        Console.WriteLine(d);
   	}
}