0

i develop winform application, i need help for this problem. when i insert to textBoxt with value 33.5 then i save my value on textBox to double variable in my program, but after my textBox convert to double, value in variable is 335 not 33.5 , this my code

private void btntambahMUN_Click(object sender, EventArgs e)
    {
        if (txtmat.Text == "")
        {
            errorProvider1.Clear();
            errorProvider1.SetError(txtmat, "Masukkan Nilai Matematika");
        }
        else if (txtipa.Text == "")
        {
            errorProvider1.Clear();
            errorProvider1.SetError(txtipa, "Masukkan Nilai IPA");
        }
        else if (txtips.Text == "")
        {
            errorProvider1.Clear();
            errorProvider1.SetError(txtips, "Masukkan Nilai IPS");
        }
        else if (txtbind.Text == "")
        {
            errorProvider1.Clear();
            errorProvider1.SetError(txtbind, "Masukkan Nilai Bahasa Indonesia");
        }
        else if (txtbing.Text == "")
        {
            errorProvider1.Clear();
            errorProvider1.SetError(txtbing, "Masukkan Nilai Bahasa Inggris");
        }
        else
        {
          /* try
            {
                con.Open();
                MySqlCommand cmd = new MySqlCommand("INSERT INTO datanilaiujian(UserID,Matematika,IPA,IPS,BIND,BING) VALUES(@a,@b,@c,@d,@e,@f)", con);
                cmd.Parameters.AddWithValue("@a",txtuseridMUN.Text.Trim());
                cmd.Parameters.AddWithValue("@b",txtmat.Text.Trim());
                cmd.Parameters.AddWithValue("@c",txtipa.Text.Trim());
                cmd.Parameters.AddWithValue("@d",txtips.Text.Trim());
                cmd.Parameters.AddWithValue("@e",txtbind.Text.Trim());
                cmd.Parameters.AddWithValue("@f",txtbing.Text.Trim());
                cmd.ExecuteNonQuery();
                con.Close();
                updatestatusUN();
                hitungrata();
                refreshMUN();       
                showallgridview();
            }
            catch (Exception ex)
            {
                con.Close();
                RadMessageBox.Show(ex.Message.ToString());
            }*/
            float coba = float.Parse(txtmat.Text);
            RadMessageBox.Show(coba.ToString());
        }
    }

any solution ?

3 Answers3

2

It seems that you have a format problem; as a matter of fact, floating point (float, double, decimal) are represented with two kind of separators: thousand and decimal ones:

123.456.789,12
   ^   ^   ^
   |   |   decimal separator
   |   |
   thousand separators      

when parsing, thousand separators are just ingnored and thus you can well have two different outcomes:

33.5  -> 335   // if '.' is a thousand separator (e.g. in Russian, ru-RU culture)
33.5  -> 33.5  // if '.' is a decimal separator  (e.g. in the US, en-US culture) 

These separators are culture dependent (e.g. en-US uses , as thousand and . as decimal when ru-RU treats them in quite the opposite way: . thousand and , decimal), that's why I suggest using CultureInfo.InvariantCulture when parsing:

float coba = float.Parse(txtmat.Text, CultureInfo.InvariantCulture);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

This might be a problem with your current culture and the NumberDecimalSeparator.

Try parsing with InvariantCulture:

float coba = float.Parse(txtmat.Text, CultureInfo.InvariantCulture);

A very similar problem can be found at C# float.Parse String

Community
  • 1
  • 1
SirBirne
  • 297
  • 2
  • 11
1

This might be because in your locale, dot is not the decimal separator.

Try setting it to the invariant culture:

        float coba = float.Parse(txtmat.Text, CultureInfo.InvariantCulture);
galister
  • 430
  • 3
  • 9