Clive Carl
Clive Carl

Reputation: 21

choosing different conversions in a c# application

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
    }

Upvotes: 0

Views: 81

Answers (4)

Nitin Varpe
Nitin Varpe

Reputation: 10694

Considering ur requirement. You can try below method

   try
{
    double.Parse(txtNumber.Text, CultureInfo.InvariantCulture)
}
catch(Exception ae)
 {
            MessageBox.Show("Not a Number")
 }

Upvotes: 0

Parv Sharma
Parv Sharma

Reputation: 12705

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

Upvotes: 1

Selman Genç
Selman Genç

Reputation: 101701

var text = txtNumber.Text;
if(text.IndexOf(',') > 0)
{
   text = text.Replace(',','.');
}

Upvotes: 0

Softlion
Softlion

Reputation: 12605

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

Upvotes: 1

Related Questions