Localization in .NET using IFormatProvider

I was doing some localization stuff in .NET / Visual C++ Managed code .

I was loading a string from a file and formatting it the current UI format .

For example , a number displayed as 12.34 ( in US English ) shall be displayed as 12,34 when the locale is of Sweden – in fact comma is the default decimal separator for most European countries.

Though the String::Format( ) provides an overloaded method with IFormatProvider, somehow the conversion never happened .

So as a workaround, I parsed the string to a double with the current UI culture format and then converted it to a string .

Maybe this is not the best way, but as it worked for me , I am posting a sample below, so it can be helpful for others.

String^ myString= “12.34”;
double tempD = double::Parse(
myString,Thread::CurrentThread->CurrentUICulture->NumberFormat );
Console::WriteLine( myString->Format(“{0}”,tempD);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: