5

I use .NET4

I use en-US locale, and want my application to work that way only.

Some of my software clients, though, are from countries (Norway - for example) where the decimal point is represented by ','.

Therefore the next line throws exception for my Norwegian client:

double a = double.Parse("1.5");

I've read I can change the CurrentCulture as follows:

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

The problem is that I have to do this for each thread seperately. Moreover, I'm not totally sure where I will implicitly parse a double, so I will have to do it on each Thread creation...

I've read that there is a solution for .NET 4.5, by changing setting the DefaultThreadCurrentCulture.

Does anyone think of a general solution for this without upgrading my .NET framework version?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user1028741
  • 2,745
  • 6
  • 34
  • 68
  • 1
    You can write your own parsing methods and specify conversion culture in those. – mcy Jan 20 '15 at 15:15
  • Where would you have implicit conversion, with a c# application? – Rowland Shaw Jan 20 '15 at 15:18
  • @mcy, I meant a more general solution, in which I don't have to find out all the places I parse double in my code... – user1028741 Jan 20 '15 at 15:20
  • @user1028741, what I meant is that sometimes in the code I call to a metadata code which does the double.parse(). That means that just looking for double.parse() in my code will not suffice... – user1028741 Jan 20 '15 at 15:22
  • Where do you get that number? User enters it? If so you should add mask/validation to prevent that. – Renatas M. Jan 20 '15 at 15:26
  • 2
    One thing to note about avoiding the upgrade is that Microsoft is ending support for .NET 4.0 Jan-2016, so not upgrading has a limited lifetime http://support2.microsoft.com/lifecycle/search/?sort=pn&alpha=.net+framework – Steve Mitcham Jan 20 '15 at 15:29
  • Could you update the metadata function you call to include the culture as well as finding the places in the code where you call parse directly? – Steve Mitcham Jan 20 '15 at 15:30
  • See http://stackoverflow.com/questions/13354211/how-to-set-default-culture-info-for-entire-c-sharp-application?rq=1 – Daniel Kelley Jan 20 '15 at 15:30
  • 1
    The question is why your client has that string `"1.5"` at all. You should store those values as `double` and if the client gives you it as input you can't change that anyway. – Tim Schmelter Jan 20 '15 at 15:32

2 Answers2

4

I know you're looking for a general solution, but the way I work around these issues is to parse using CultureInfo.InvariantCulture:

double a = double.Parse("1.5", CultureInfo.InvariantCulture);

Beware though, this my cause issues when your string contains both commas and periods.

Check the answers at the following link:

How do I parse a string with a decimal point to a double?

Community
  • 1
  • 1
Vlemert
  • 316
  • 2
  • 5
  • Not really "workaround", but rather "correct approach". Specifying culture explicitly is indeed the right solution when you know what culture is used. And `InvariantCulture` seem to be the one OP should be using as data likely comes from some other place than user input. – Alexei Levenkov Jan 20 '15 at 15:34
-3

i think i understood your problem and this could be done without upgrading your .Net i hope this helps as i was making a program i made a inital setup for my program in which i ask so make a Dialog that will ask the user for a country and then you add your decimal code as a case statement.here :-

void AskForACountry() { frmEdit = new Form(); frmEdit.ShowIcon = false; cmbxLang = new ComboBox(); frmEdit.Size = new Size(199, 113); Button btnSetLang = new Button(); btnSetLang.Size = new Size(88, 23); btnSetLang.Text = "Set Language"; btnSetLang.Location = new Point(40, 39); cmbxLang.Location = new Point(28, 12); cmbxLang.DropDownStyle = ComboBoxStyle.DropDownList; String[] arrayCountry = { "Catalan - Spain", "Chinese - China", "Chinese - Hong Kong", "Chinese - Taiwan", "Danish - Denmark", "Dutch - Netherlands", "English - Australia", "English - Canada", "English - Great Britain", "English - US", "Finnish - Finland", "French - Canada", "French - France", "German - Germany", "Italian - Italy", "Japanese - Japan", "Korean - Korea", "Norwegian - Norway", "Polish - Poland", "Portuguese - Brazil", "Portuguese - Portugal", "Russian - Russia", "Spanish - Mexico", "Spanish - Spain", "Swedish - Sweden" }; cmbxLang.DataSource = arrayCountry.ToList(); btnSetLang.Click += new EventHandler(btnSetLang_Click); frmEdit.Controls.Add(cmbxLang); frmEdit.Controls.Add(btnSetLang); MessageBox.Show("Please select a country that fits your computer's default language and dialect.", "Select A Country"); frmEdit.StartPosition = FormStartPosition.CenterScreen; frmEdit.AcceptButton = btnSetLang; frmEdit.ShowDialog(); }