Habib Wisnu P Habib Wisnu P - 4 years ago 154
C# Question

convert decimal from textbox to variable with 2 decimal point

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 ?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download