Search code examples
.netstring-formattingglobalizationtostring

How do I change the culture used by Object.ToString?


It's tedious to write

string msg = "Result is " + r.ToString(CultureInfo.InvariantCulture) + " with "
    + p.toString(CultureInfo.InvariantCulture) + "% probability.";

I would prefer

string msg = "Result is " + r + " with " + p + "% probability.";

How can I set InvariantCulture as the default throughout my project?

My goal is to produce programmer readable output.


Solution

  • You need to set the current thread and ui thread cultures to it:

    Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
    Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;