none
How do I get Decimal.Parse to stop taking current culture into account? RRS feed

  • Question

  • How do I get Decimal.Parse to stop taking current culture into account?

     

    I tried the following but I get an error saying “String was not recognized as a valid DateTime.

     

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");

     

    Decimal.Parse("5000.000,51",CultureInfo.InvariantCulture)

    Thursday, July 29, 2010 9:49 PM

Answers

  • Yes, I was misunderstanding what invariant culture was providing. Thank you for the clarification and possible ways to solve the issue. To solve the issue, the UI will be passing me the culture settings
    • Marked as answer by Demi100 Friday, July 30, 2010 6:47 PM
    Friday, July 30, 2010 6:47 PM

All replies

  • I cannot understand how you would an invalid  DateTime error calling decimal.Parse(), so I assume that was a typo.

    As for the actual issue, the invariant culture uses '.' as a decimal separator and ',' as a thousand separator. Under these conditions, the string "5000.000,51" does not constitute a valid decimal value.

    Try: decimal.Parse ("5,000,000.51", CultureInfo.InvariantCulture);

    HTH
    --mc

    Thursday, July 29, 2010 10:06 PM
  • Yes it was a  copy and paste error. 

    Well, I don't know how de the decimal string is being passed to me. I will be getting info from a multi language page.  I just wanted to understand if decimal string could be in any format pattern(different cultures) and  invariantculture format provider will ignore the culture. My understanding from MSDN is that Inavariant culture is culture-insensetive.

     

    Friday, July 30, 2010 3:34 PM
  • I'm afraid you misunderstood what the invariant culture is providing.

    CultureInfo.InvariantCulture allows your program to disregard the culture settings of your computer or thread, and use a generic English culture instead (this is documented in the MSDN article on CultureInfo.InvariantCulture in the Remarks section). Aside of providing an arbitrary standard that promotes interoperability between computers using different regional settings, the Invariant Culture also mimics the behavior of some well known standard libraries that weren't culture-aware.

    Back to your issue, if you don't know the culture that was used to format your numbers (or dates, for that matter), it is generally impossible to parse it unambiguously. For instance, "1,234" would parse to 1234 in en-US, but would parse to 1.234 in it-IT. There is simply no way to tell them apart unless, as I mentioned, you know the culture in advance.

    There might be a solution if you only deal with some special case. If you know, for instance, that your decimal numbers will always have a mantissa, you can determine which is the decimal separator by looking at the last separator in the string (for instance using String.LastIndexOfAny) and then either convert your string to the invariant culture, or passing a suitable culture to decimal.Parse.

    This might work for numbers, but there is no special case I can think of to distinguish unambiguously between date formats (I am mentioning this since you seem to be dealing with dates as well). If that's the case, you will need your page to provide the culture and this will indirectly solve the decimal problem as well.

    HTH
    --mc

    • Proposed as answer by Marcel Roma Friday, July 30, 2010 5:43 PM
    Friday, July 30, 2010 5:11 PM
  • Yes, I was misunderstanding what invariant culture was providing. Thank you for the clarification and possible ways to solve the issue. To solve the issue, the UI will be passing me the culture settings
    • Marked as answer by Demi100 Friday, July 30, 2010 6:47 PM
    Friday, July 30, 2010 6:47 PM
  • Hi Mario,

        Do you know if there is a .NET library that I could use to parse datetime and that is culture independent?

    Thursday, August 26, 2010 8:10 PM