0

I'm trying to make my program more compatible, for that I've ended up changing a lot of little things for example,

Using textBox.Text = Convert.ToString(value) instead of = "value"

Getting the current user decimal separator and using it replace on a tryparse

char sepdec = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);

float.TryParse(str.Replace(",", sepdec.ToString()).Replace(".", sepdec.ToString()), out testvariable;

But these solutions are hard to implement when you have already coded most of your program without worrying about it.

So I'm trying to find ways to make the whole code compatible, without having to edit every tryparse and every textbox

I've tried to do the following:

//Get the current user decimal separator before the program initializes
char sepdec = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);

//Create a current culture clone and change the separator to whatever the user has in his regional options, before the initializing the component

  public Form1()
  {
  System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
  customCulture.NumberFormat.NumberDecimalSeparator = sepdec.ToString();

  System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

  InitializeComponent();
  }

But I've tested this, and it's not really doing anything. Wasn't it supposed to make the program understand something like ok, now you use dot as your decimal separator altough you have values in textBox as "2,5" ?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
ng80092b
  • 621
  • 1
  • 9
  • 24

1 Answers1

1

ok, now you use dot as your decimal separator altough you have values in textBox as "2,5"

Exactly.

float.TryParse method uses your CurrentCulture settings if you don't use any IFormatProvider with it.

If you try to parse "2,5" to float without any IFormatProvider, your CurrentCulture has to have , as a NumberDecimalSeparator.

If you try to parse "2.5" to float, you either use a culture as an another parameter which already has . as a NumberDecimalSeparator (like InvariantCulture), or you can .Clone() your CurrentCulture (as you did) and set it's NumberDecimalSeparator property to . and use this cloned culture as an another parameter in float.TryParse overload.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • I think I didn't explain myself correctly because it doesn't work. When I say `ok, now you use dot as your decimal separator altough you have values in textBox as "2,5"` What I mean is`you will now treat "2,5" as "2.5" (with a dot), and tryparse it correctly`. – ng80092b Apr 05 '15 at 19:19
  • Also, I might be wrong again, but this could be the issue. When I say `System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;` Shouldn't every `tryparse` in the code use that customculture? Because if I have to use `currentculture` as a argument, it will defeat the purpose of avoiding editing every `tryparse` – ng80092b Apr 05 '15 at 19:22
  • @ng80092b No. A culture _can't_ treat `2,5` as `2.5` or vice versa. A float can have different representation as a string based on culture settings. This is the same as when you try parse a string to float. Your string needs to exact separators that used in current culture for parsing operation. – Soner Gönül Apr 05 '15 at 19:34
  • 1
    @ng80092b For your second comment, take a look: http://stackoverflow.com/questions/468791/is-there-a-way-of-setting-culture-for-a-whole-application-all-current-threads-a – Soner Gönül Apr 05 '15 at 19:35