JamesT
JamesT

Reputation: 83

Language and Locale settings in .NET Framework 4.0

We have a Winform application that gets some numeric values from a database. This works fine when the users version of windows is English. So number formats are all perfect. However when the same application is opened in Windows 7 with Portuguese as the base language (Portuguese-Brazil), the numbers are all formatted wrong. This is because US English and Portuguese formats for numbers are totally different.

It appears this change has occurred recently in .NET Framework 4.0 since the application was working perfectly when it was built using 2.0 Framework.

Example, the number "THOUSAND" will show up as 1.000,00 which is being interpreted as ONE in the system running the Portuguese version of windows.

English: 1,000.00 = Thousand

Portuguese: 1.000,00 = Thousand

Can someone point me to any resources on how to work around this or what is the correct method to force an application to use database values instead of formatting them to the local users' system? The users are OK with numbers being displayed in the US Format.

Upvotes: 1

Views: 1399

Answers (2)

Artur Udod
Artur Udod

Reputation: 4743

The format (delimmeters and spaces) of displayed numbers is a matter of their string representation. Decimals are stored in DB or memory without delimeters (obviously), they are just 128bits on your hard disk (roughly saying). The format comes to scope when you try to display them. If your number are stored in DB in correct numeric format, then this should not be a problem.

The problem may occur if somewhere you try to convert a string to number. For instance if you store values in db as string (I hope you dont). In this case you should specify the format provider each time you convert numbers to string and vice versa. For instance:

decimal.Parse(numberAsString, CultureInfo.InvariantCulture); // 1
decimalNumber.ToString(CultureInfo.InvariantCulture); // 2

reference 1 reference 2

Upvotes: 1

Georgi-it
Georgi-it

Reputation: 3686

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

this will reset the culture, use it at the start of your form/program

or

Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

you can try both as i am not sure which one is gonna work

Upvotes: 2

Related Questions