Formatting/parsing for a specific culture

Sometimes you may want to use a specific format for formatting and parsing of textual data.  The easiest way to do this is to select a specific culture and use that with formatting and parsing methods.  Unfortunately, the CultureInfo constructor that just takes the name of the culture defaults to accepting any overrides the current user has set in Regional and Language Options.

For example, if in Regional and Language Options you have English (United States) selected and you’ve changed the currency symbol to be something other than the dollar sign ($), the following won’t use the dollar sign:

            Decimal d = 2123.5M;
            String text = String.Format(new System.Globalization.CultureInfo(“en-us”), “{0:c}”, d);

If you want to use a specific culture, you must use the CultureInfo constructor overload that accepts a Boolean so you can tell it not to use the user overrides.  For example, the following will result in a dollar sign, despite what the user has done in Regional and Language Options:

            Decimal d = 2123.5M;
            String text = String.Format(new System.Globalization.CultureInfo(“en-us”, false), “{0:c}”, d);


2 thoughts on “Formatting/parsing for a specific culture

  1. Interesting – I didn’t know that. I wonder why the default is to use computer-dependent behaviour as typically you create specific cultures (as opposed to just using CurrentCulture or CurrentUICulture) in server applications where you want to render the results in the culture the client is expecting, and of course you would want consistent behaviour across all machines in your server farm!

    I tend to use CultureInfo.CreateSpecificCulture to create culture objects; I’ll have to have a dig around with Reflector to see what this does with the UseUserOverride setting.

  2. I’m not sure why it does it that way. I doubt anyone on the BCL team knows… It’s not very intuitive.

    Unfortunately, CreateSpecificCulture does the same thing, except CreateSpecificCulture has no way of choosing whether to accept user overrides or not…

Leave a Reply to PeterRitchie Cancel reply

Your email address will not be published. Required fields are marked *