Problems with "decimal symbol", in Region and Language for calculations program

749 views Asked by At

I'm currently developing a calculations program for a company in Europe. They are using the metric system and as default the "," as a decimal symbol.

My program calculates different stuff and some of the calculations are triggered by TextChanged events from textboxes in C#/WPF. All worked fine until a couple of days ago when a user complained about that on some of the calculations nothing happened when he typed a number in the textbox. There were no errors or nothing just plain absent of results in the result boxes (for some of the calculations). I discovered that for some reason a textbox with the default value (set by me) "0,05" was not shown, the box was blank. But further down there were values of "0,5" showing (also set by me). I was very confused about this and started debugging and searching for possible causes for this to happen, but found nothing. After a while I realized that he had changed his "decimal symbol" under Region and Language from "," to ".". This was done in order to make a program from the US to work, because that program crashed upon startup if this was not done! So there I had it, change it back and it all worked fine. But this is not desired because the user does not want to have to switch back and forward with this setting so I need to find a solution for this.

I have a method in the "PreviewTextInput" that was created in order to make sure that it is a number and only contains one decimal separator. I do not see how this could be the cause of it or do you?

private void CheckIsNumeric(TextCompositionEventArgs e, object sender)
    {
        int result;

        if (!(int.TryParse(e.Text, out result) || e.Text == ","))
        {
            e.Handled = true;
        }
        if (e.Text == "," && (sender as TextBox).Text.IndexOf(',') > -1)
            e.Handled = true;

    }

Why is the value "0,5" present upon startup but "0,05" is "deleted" and most likely the one that fucks it all up? Is there a way to solve this problem, I need some fresh ideas because I'm currently stuck in my mind about altering something in my program depending on the Region and Language.

Any help is appreciated.

Update:

If I change my settings in Region and language "Format" from Swedish to English it works fine either way with the "decimal symbol". The program behavs the same way if I set it to "," or "." Why does it not work when using Swedish and have the "." value?

1

There are 1 answers

4
Jeroen van Langen On

I would do something like this:

string[] numbers = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

// check numbers.
if(numbers.Contains(e.Text))
{
    e.Handled = true;
    return;
}

// check separator only occurs ones.
if(e.Text == ",")
{
    e.Handled = (((TextBox)sender).Text.IndexOf(',') == -1);
    return;
}

Given that the e.Text is the char that is typed. If e.Text is the complete text, you could try to use:

double result;

// any fixed culture that uses a , as decimal separator.
CultureInfo cultureInfo = new CultureInfo("nl-NL");

e.Handled = double.TryParse(e.Text, NumberStyles.Float, cultureInfo, out result);