简体   繁体   中英

How do I Convert Textbox to decimal

I want to convert a code in datagrid textbox to decimal but don't just know how to. I have search the net and this forum but not exactly whai I want. Any help would be appreciated Below is the code:

public void LoadStock() {
  int i = 0;
  dataGridView2.Rows.Clear();
  cn.Open();
  cm = new SqlCommand("select * from vwStockIn where RefNo like '" + txtRefNo.Text + "' and Status like 'Pending' ", cn);
  dr = cm.ExecuteReader();
  while (dr.Read()) {
    i++;
    dataGridView2.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), dr[5].ToString(), dr[6].ToString(), dr[7].ToString(), dr["CompanyName"].ToString(), dr["AccBalance"].ToString());
  }
  dr.Close();
  cn.Close();
}

To covert the text in the texbox to a decimal first you have to check if the text in the text box is a number. For that you can just...

bool isNumeric = float.TryParse(YourTextBox.Text, out float n);

After this, "n" will hold the value of the decimal, but optionally you can just...

if (isNumeric)
{
    Nullable<Double> yourNewNum = Convert.ToDouble(YourTextBox.Text);
} 
else 
{
    // Your else code here.
}

您可以将其解析为十进制:

decimal.Parse(your_string_variable);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM