DecimalSeparator

Hi,

I got 20 textboxes and i want to change the decimal separator to the one they have in their region settings.

This is how i tried it:

foreach (Control textbox in this.Controls)
{
if (textbox is TextBox)
{
int controlIndex = 0;
for (int k = 0; k < 20; k++)
{
string decimalSeparator = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;

string input = this._textBox[k].Text;
input = input.Replace(".", decimalSeparator);
input = input.Replace(",", decimalSeparator);

double d;
if (double.TryParse(input, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.CurrentCulture, out d))
{
this._tempAnswer = d;
this._answer[controlIndex] = this._tempAnswer;
}
}
}
}

I accept 2 separators which are the dot and the comma and the application should check which one is used in their region settings.

But somehow this is not working. When i press the dot instead of the comma, its marked as wrong answer.

What am i doing wrong


Answer this question

DecimalSeparator

  • ben8coast

    Hi RizwanSharp,

    That worked, thank you very much!

  • BarrySumpter

    Try this Please:

    string decimalSeparator = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
    foreach (Control textbox in this.Controls)
    {
    TextBox currentTextBox = textBox as TextBox;

    if(currentTextBox != null)

    {
    currentTextBox.Text = currentTextBox.Text.Replace(".", decimalSeparator);
    currentTextBox.Text = currentTextBox.Text.Replace(",", decimalSeparator);
    }
    }

    It'll replace all the . and commas to appropriate decimel seperators! Now you can add your code to see if its Decimel or string...

    I hope this will work!

    Best regards,

    Rizwan



  • Will Merydith

    You are more than welcome :)!

    Best Regards,

    Rizwan



  • AnthonyAM444

    You should left the .Net framework handle it:



    NumberFormatInfo enUS = CultureInfo.GetCultureInfo("
    en-US").NumberFormat;
    NumberFormatInfo deDE = CultureInfo.GetCultureInfo("
    de-DE").NumberFormat;

    string num = "234.56";

    double num2= double.Parse(num, enUS);
    string num3 = num2.ToString(deDE);

    Console.WriteLine(num3);



  • DecimalSeparator