Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am facing localization issue with my Silverlight application. I am working on one financial application and showing the company reported data on grid. I am trying to implement the localization stuffs, having en-US (English - United States) as default localization, but i want to change the localization to es-AR (Spanish - Argentina) and i am facing the following issue.

My value is 0.21

In en-US it is represented as 0.21 and In es-AR it is represented as 0,21

When my machine localization is es-AR Convert.ToDouble("0.21") is getting converted as 21.0

I have searched on internet and found that i need to pass the culture number format, still having the same issue

C#
double dblValue = Convert.ToDouble("0.21");

// Out Put is 21.00

C#
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-AR");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-AR");

numFormatInfo.NumberDecimalSeparator = ",";
numFormatInfo.NumberGroupSeparator = ".";

double dblValue = Convert.ToDouble("0.21", numFormatInfo);

// Out Put is same again 21.00

Also the following statements throws me error of invalid number format on "es-AR" culture

C#
double dblValue = Convert.ToDouble("86,131,789");
double dblValue = Convert.ToDouble("86,131,789", numFormatInfo);

// Throws Error -
VB
System.FormatException: Input string was not in the correct format.
en System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
en System.Convert.ToDouble(String value)



So how should i achieve this ?
Posted
Comments
Henry.Ayoola 2-Aug-11 8:37am    
What are you actually trying to do? Ensure that both "0.21" and "0,21" are parsed as 21/100 regardless of current culture?

try this:
C#
CultureInfo myUSCulture = new CultureInfo("en-US");
                CultureInfo myUKCulture = new CultureInfo("en-GB");
                CultureInfo myZACulture = new CultureInfo("af-ZA");
                Double waarde = Convert.ToDouble(textBox2.Text, Thread.CurrentThread.CurrentCulture.NumberFormat);

                MessageBox.Show(waarde.ToString("C", myUSCulture));
                MessageBox.Show(waarde.ToString("C", myUKCulture));
                MessageBox.Show(waarde.ToString("C", myZACulture));
                MessageBox.Show(waarde.ToString("C", Thread.CurrentThread.CurrentCulture));


you want specific culture and not culture from thread
 
Share this answer
 
The behaviour you described is correct, and you should respect the local number format settings with any UI controls. Just using Double.Parse and Double.ToString (or Decimal or whatever number type you are using) will work fine for local interactions.

You should only need to specify a culture if you are transforming numbers to or from a canonical string format that needs to work on all locales (for example files). In that case you should typically use CultureInfo.InvariantCulture to do the formatting. In a Silverlight app this is not something that you should need to do much, though (if you use a WCF service to communicate with the host web app, all the string transformations are handled for you and you don't have to care).
 
Share this answer
 
Comments
Yogesh Potdar 2-Aug-11 8:35am    
In my application, my user define the NoOfDecimal, wheather he wants to show currency or not .. so i need to do string formatting as per user settings which is causing me issue, and to avoid the load on GUI while displaying the data i am doing the same on my offline applications and just showing it on GUI
BobJanova 2-Aug-11 10:30am    
You can just use double.ToString to format the numbers. That is intrinsically a UI level thing, you should not be storing numbers in a specific string format that has localisation issues elsewhere. Formatting cells within a grid is not going to cause any 'load' that anyone will notice.
Yogesh Potdar 2-Aug-11 10:33am    
http://interactive.indigotools.com/indigoweb/default.aspx?ticker=ndaq&type=3&exchange=nasdaqgs

This is what i am working on currently .. Default localization is en-US
When you set thread cultures, numbers are formatted and read according to the rule for that culture (at least when using double.ToString("N0") and double.Parse (or double.TryParse).

On a web application, you will typically set the thread culture before each request if you load localized string from resources or works with numbers.

If you uses predefined culture, you don't have to modify numFormatInfo and you can pass the CultureInfo object if you want to use a culture that if different than the thread culture.

As mentionned in another solution, you would typically uses the InvariantCulture when you need to save a value in a file (like an Excel file using Open XML SDK).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900