Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert string to decimal?

Tags:

c#

I have this textbox that accepts numbers, commas, and periods.

Let's say this textbox contains input 14,500.00

I tried to convert this number to decimal with Convert.ToDecimal(textbox.text) but it's not working. Convert.ToDecimal() to textboxes that contain input that has the format XXXX.DD are converted to decimal but input with format X,XXX.DD or any input with a thousand separator results to error:

Input string was not in correct format

Is Convert.ToDecimal() appropriate in this case?

ADDITIONAL INFO:

enter image description here

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

The syntax in the 'Add' button includes:

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

The Amount field in my SalesOrderDetails table has the datatype decimal(18,2)

like image 944
Leonard Cupat Avatar asked Feb 18 '14 14:02

Leonard Cupat


People also ask

How do you convert a string to a numerical value?

The unary plus operator ( + ) will convert a string into a number. The operator will go before the operand. We can also use the unary plus operator ( + ) to convert a string into a floating point number. If the string value cannot be converted into a number then the result will be NaN .

How do you convert an empty string to a decimal?

It's not possible to convert an empty string to a number (decimal or not). You can test for an empty string before trying the conversion or use decimal. TryParse() - I think that was available in 1.1.

How do you convert string to decimal in Python?

literal_eval() function to convert the hexadecimal string to decimal string. convertion = literal_eval(testing_string) # At last, print result. print ("The converted hexadecimal string into decimal string is: " + str(convertion))


3 Answers

You can force a culture and use decimal.Parse

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

Is Convert.ToDecimal() appropriate in this case?

Yes, you could also continue to use Convert.ToDecimal if you want:

d = Convert.ToDecimal("14,500.00", CultureInfo.InvariantCulture);
like image 131
Tim Schmelter Avatar answered Oct 24 '22 02:10

Tim Schmelter


I would give decimal.TryParse a go

decimal d;
if(decimal.TryParse(textbox.Text, out d))
{
//do something
}
like image 4
matt_lethargic Avatar answered Oct 24 '22 04:10

matt_lethargic


I suspect you're using a culture that defines . as the thousands separator and , as the decimal separator. If you want to force , and . as the thousands and decimal separators, respectively then use:

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

Is Convert.ToDecimal() appropriate in this case?

It's fine - the main difference is it supports more types than decimal.Parse, which only supports strings.

like image 1
D Stanley Avatar answered Oct 24 '22 04:10

D Stanley