0

I have a c# application that convert lengths. The application works and gives correct results if a user enters a number separated by a dot (3.45) but if a user puts a comma instead (3,45) it gives wrong results. Does anyone know how i can counteract the problem

    private void butCal_Click(object sender, EventArgs e)
    {
        // initialising and declaring variables 

        double miles, kilometres, feet, metres, inches, centimetres = 0;

        // the if statements initiates an event handler to select items from the conversions (combobox) to do conversion

        if (Conversions.Text == "Miles to Kilometres")
        {
            if (!double.TryParse(txtNumber.Text, out miles)) // verifies that the string entered is a number
            {
                MessageBox.Show("Please enter a number !"); // displays message if no number is entered
            }

            // Logic for doing calculations
            kilometres = miles * 1.6093; 
            txtAnswer.Text = kilometres.ToString("N2") + " Kilometres"; // displays the answer and rounds it off to two decimals

            txtAnswer.Visible = true;
            txtAnswer.Enabled = false;
            lblAnswer.Text = " Answer = ";
            lblAnswer.Visible = true;
        }

        else if (Conversions.Text == "Kilometres to Miles")
        {
            if (!double.TryParse(txtNumber.Text, out kilometres))
            {
                MessageBox.Show("Please enter a number.");
            }
            miles = kilometres * 0.621;
            txtAnswer.Text = miles.ToString("N2") + " miles ";

            txtAnswer.Visible = true;
            txtAnswer.Enabled = false;
            lblAnswer.Text = " Answer = ";
            lblAnswer.Visible = true;
        }
        else if (Conversions.Text == "Feet to Metres")
        {
            if (!double.TryParse(txtNumber.Text, out feet))
            {
                MessageBox.Show("Please enter a number.");
            }
            metres = feet * 0.3048;
            txtAnswer.Text = metres.ToString("N2") + " metres ";

            txtAnswer.Visible = true;
            txtAnswer.Enabled = false;
            lblAnswer.Text = " Answer = ";
            lblAnswer.Visible = true;
        }

        else if (Conversions.Text == "Metres to Feet")
        {
            if (!double.TryParse(txtNumber.Text, out metres))
            {
                MessageBox.Show("Please enter a number.");
            }
            feet = metres * 3.280839895;
            txtAnswer.Text = feet.ToString("N2") + " Ft ";

            txtAnswer.Visible = true;
            txtAnswer.Enabled = false;
            lblAnswer.Text = " Answer = ";
            lblAnswer.Visible = true;
        }

        else if (Conversions.Text == "Inches to Centimetres")
        {
            if (!double.TryParse(txtNumber.Text, out inches))
            {
                MessageBox.Show("Please enter a number.");
            }
            centimetres = inches * 2.54;
            txtAnswer.Text = centimetres.ToString("N2") + " cm ";
        }

        else if (Conversions.Text == "Centimeters to Inches")
        {
            if (!double.TryParse(txtNumber.Text, out centimetres))
            {
                MessageBox.Show("Please enter a number.");
            }
            inches = centimetres * 0.3937;
            txtAnswer.Text = inches.ToString("N2") + " Inches ";

            txtAnswer.Visible = true;
            txtAnswer.Enabled = false;
            lblAnswer.Text = " Answer = ";
            lblAnswer.Visible = true;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // resetting all labels, text fields and the conversion (combobox)
        Conversions.SelectedIndex = -1;
        txtNumber.Clear();
        txtAnswer.Clear();
        butCal.Visible = false;

        // Hidding text fields and labels until they are called
        txtAnswer.Visible = false;
        txtNumber.Visible = false;
        lblAnswer.Visible = false;
        lblNumber.Visible = false;
    }

    private void butExit_Click(object sender, EventArgs e)
    {
        Close(); // exits the application
    }
Clive Carl
  • 21
  • 7

4 Answers4

1

double.TryParse has an overload taking a Culture or number styles.

http://msdn.microsoft.com/en-us/library/3s27fasw(v=vs.110).aspx

Specifically for IFormatProvider provider parameter you may use CultureInfo.InvariantCulture for the dot

Softlion
  • 12,281
  • 11
  • 58
  • 88
1

if you are specifically talking about (3,45) its ok to throw an error because 3,45 probably means nothing. and still if you want it to work then use solution by @selman22 else if you are confirm that the input entered by the user will be one of the standards ie that the , will be used to seperate thousands then use the solution given here Convert.ToInt32() a string with Commas which is similar for double or the solution given by @Softlion

Community
  • 1
  • 1
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
  • The problem here is that `3,45` is still considered a valid number. Depending on the user's regional settings, it will be (mis)interpreted as either `3.45` or `345`. So when do you throw the error? – Steven Liekens Jan 23 '14 at 07:41
0
var text = txtNumber.Text;
if(text.IndexOf(',') > 0)
{
   text = text.Replace(',','.');
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • What if something like this is entered `1,23.3` :)? Why even bother - answer to everything is 42 so `text = "42"` guarantees correct parsing and reliable result. – Alexei Levenkov Jan 23 '14 at 07:14
  • it's a special case :D then we need to make some extra validation,but OP didn't ask about it. – Selman Genç Jan 23 '14 at 07:16
0

Considering ur requirement. You can try below method

   try
{
    double.Parse(txtNumber.Text, CultureInfo.InvariantCulture)
}
catch(Exception ae)
 {
            MessageBox.Show("Not a Number")
 }
Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60