Home > .net 2.0, c#, Visual Studio 2005 > IFormatProvider and Globalization

IFormatProvider and Globalization

April 30, 2007

So, as I continue through with FXCop (yes, I never really dinked with this tool before yesterday, sue me), I find odd little things that make absolutely no sense until I dig down (and even then, perhaps no sense).

One of those?  The touchiness of “Specify IFormatProvider”:

“Students.get_AdmissionDate():DateTime makes a call to System.Convert.ToDateTime(System.String) that does not explicitly provide an IFormatProvider. This should be replaced with a call to System.Convert.ToDateTime(System.String,System.IFormatProvider).”

Okay, I get that.  I get that Dates and Times are sensitive to the globalization stuff.  I can fix that by modifying my conversion helper function to.

DateTime.Parse((string)convertible, CultureInfo.CurrentCulture); 

// Note: null can be used for the culture for Current.

Now, what I don’t get is when it throws it for Integers. Currency, yeah, definately. Decimals—yeah, I see that.  DateTime, sure, why not.  But integers?  A whole number without much flair… <boggle />

Tags: ,

  1. May 1, 2007 at 8:38 pm

    Because numbers look different in different cultures, for example, in the US a number would typically look like (ie via int.ToString()):

    1,000

    However, the same number in Germany would look like:

    1.000

  2. May 1, 2007 at 9:39 pm

    Oh, that makes sense. For some reason, I had it in my mind that integers wouldn’t show any delimiters without custom formatting applied at the presentation layer.

    Is that so without the globalization specifications?

    Without: 1000
    US: 1,000
    GER: 1.000

    ?

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: