41

I am trying to parse string "0.0000" with double.TryParse() but I have no idea why would it return false in this particular example. When I pass integer-like strings e.g. "5" it parses correctly to value of 5 .

Any ideas why it is happening ?

3
  • 8
    it take the localization settings at runtime into account... perhaps you are running whis on a system where . is not the decimal point but , instead ?
    – Yahia
    Dec 11, 2011 at 11:28
  • 2
    @Yahia: Suggest you make that an answer (and include overload that takes an IFormatProvider so the culture can be specified).
    – Richard
    Dec 11, 2011 at 11:30
  • Perhaps you chose the wrong culture. TryParse uses the current culture by default. (An idiotic decision IMO, but it's too late now to change it) Dec 11, 2011 at 11:31

9 Answers 9

67

it takes the localization settings at runtime into account... perhaps you are running this on a system where . is not the decimal point but , instead...

In your specific case I assume you want a fixed culture regardless of the system you are running on with . as the decimal point:

double.TryParse("0.0000", NumberStyles.Number, CultureInfo.CreateSpecificCulture ("en-US"), out temp)

OR

double.TryParse("0.0000", NumberStyles.Number,CultureInfo.InvariantCulture, out temp)

Some MSDN reference links:

10

TryParse uses the current culture by default. And if your current culture uses a decimal seperator different from ., it can't parse 0.0000 as you intend. So you need to pass in CultureInfo.InvariantCulture.

var numberStyle = NumberStyles.AllowLeadingWhite |
                 NumberStyles.AllowTrailingWhite |
                 NumberStyles.AllowLeadingSign |
                 NumberStyles.AllowDecimalPoint |
                 NumberStyles.AllowThousands |
                 NumberStyles.AllowExponent;//Choose what you need
double.TryParse( "0.0000", numberStyle, CultureInfo.InvariantCulture, out myVar)
0
3

Almost certainly the problem is that Thread.CurrentCulture does not use dot as the decimal separator.

If you know that the number will be always formatted with dot as the decimal separator, use this code that utilizes the other overload of double.TryParse:

style = NumberStyles.Float | NumberStyles.AllowThousands;
culture = CultureInfo.InvariantCulture;
float num;
if (double.TryParse("0.0000", style, culture, out num)) {
    // whatever
}
2

It will return false if the current culture specifies a decimal point separator that is different than the . character.

When parsing strings representation you need to be aware in what culture they are represented otherwise you'll get unexpected behavior.

In this case you're getting false, but it could even be worse, for example in the following example if you were expecting to get the number one you would instead get one thousand:

Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-PT");

double d;
Console.WriteLine(double.TryParse("1.000", out d));
Console.WriteLine(d);

This is because in the pt-PT culture the . character is used as NumberGroupSeparator and the , character is used as NumberDecimalSeparator.

If the input you're parsing comes from the user then always parse it using the culture the user is associated. Getting the culture the user is associated is something dependent on the context, for example in a Windows Forms application you would use Thread.CurrentThread.CurrentCulture when on the UI thread to get it. In a ASP.NET application this may be different.

1

To change the culture to something that has "." as decimal separator use:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
2
  • Changing the current culture is a bit ugly. Especially since you should save and restore it. And then you need to take care of exceptions... Dec 11, 2011 at 11:38
  • @CodeInChaos: I was thinking that maybe he needs app-wide, or at least thread-wide parsing of doubles in the same manner. So it helps him to avoid passing the culture to every parse method he calls.
    – Tudor
    Dec 11, 2011 at 11:40
1

When this method returns, contains the double-precision floating-point number equivalent to the s parameter, if the conversion succeeded, or zero if the conversion failed.

From the MSDN page for this method.

http://msdn.microsoft.com/en-us/library/994c0zb1.aspx

Zero goes in, zero comes out.

0

Changing the CultureInfo with TryParse did nothing for me. I had to change the number formats under Control Panel (Change, date, time, or number formats) and changed the Decimal symbol. Than it worked again.

-2

The most stupid thing about this is that it only accept "," as decimal separator in my culture, but the result is with a ".". Someone, somewhere, wasn't very lucky thinking that day... So I'll try this:

if ((double.TryParse(sVat, out vat)) == false)
    if (double.TryParse(sVat.Replace(",", "."), out vat) == false)
      double.TryParse(sVat.Replace(".", ","), out vat);
1
  • 1
    "I'll try this" Is this an answer? Does it work? If don't say "I'll try this". Sep 13, 2017 at 16:17
-3

It works for me:

double temp = 0;
Console.WriteLine(double.TryParse("0.0000", out temp));
Console.ReadLine();

writes True.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.