How to convert string to decimal?

I have this text box that accepts numbers, commas and periods.

Say this text box contains an input 14,500.00

I tried to convert this number to decimal s Convert.ToDecimal(textbox.text), but it does not work. Convert.ToDecimal()into text fields containing input that have a format XXXX.DD, are converted to decimal, but entered with a format X,XXX.DDor any input with thousands of separator results for the error:

Input string was not in correct format

Is it Convert.ToDecimal()suitable in this case?

ADDITIONAL INFORMATION:

enter image description here

Here is the form. If I click "Add", the product "Price" and "Quantity" should be displayed as "Amount" in the datagridview.

The syntax for the Add button includes:

DataRow dr;
dr = dsDetail.Tables["SalesOrderDetails"].NewRow();
dr["Amount"] = Convert.ToDecimal(txtSellingPrice.Text) * Convert.ToDecimal(txtQuantity.Text);

Amount SalesOrderDetails decimal(18,2)

+3
3

decimal.Parse

decimal d = decimal.Parse("14,500.00", CultureInfo.InvariantCulture); // 14500

Convert.ToDecimal() ?

, Convert.ToDecimal, :

d = Convert.ToDecimal("14,500.00", CultureInfo.InvariantCulture);
+6

decimal.TryParse a go

decimal d;
if(decimal.TryParse(textbox.Text, out d))
{
//do something
}
+2

, , . , , - . , . , :

decimal value = Convert.ToDecimal(textbox.text,CultureInfo.InvariantCulture);

Convert.ToDecimal() ?

- , , decimal.Parse, string s.

+1

All Articles