简体   繁体   中英

How do I convert string to decimal?

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:

在此处输入图片说明

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)

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);

I would give decimal.TryParse a go

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

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 string s.

I agree with @matt_lethargic, but offer a more complete solution. Tested with XUnit :)

[Theory]
[InlineData("en-US","44.00")]
[InlineData("es-PE", "44,00")]
[InlineData("es-PE", "44.00")]
[InlineData("es-PE", "0.01E-15")]
[InlineData("es-PE", "0,01E-15")]
public void ParsesDeciaml(string culture, string dec)
{
    CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
    decimal d;
    if (!decimal.TryParse(dec, out d)
        && !decimal.TryParse(
                dec,
                System.Globalization.NumberStyles.Any,
                System.Globalization.CultureInfo.CurrentCulture,
                out d
            )
        && !decimal.TryParse(
                dec,
                System.Globalization.NumberStyles.Any,
                System.Globalization.CultureInfo.InvariantCulture,
                out d
            )
    ) Assert.False(true, dec);

}

That way you can capture values with exponential formats.

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