7

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)

D Stanley
  • 149,601
  • 11
  • 178
  • 240
Leonard Cupat
  • 113
  • 1
  • 2
  • 11
  • 1
    What is your culture? – Soner Gönül Feb 18 '14 at 14:34
  • @SonerGönül How can I determine the culture? – Leonard Cupat Feb 20 '14 at 06:34
  • You can use `CultureInfo.CurrentCulture` property the culture you are using. This property uses your _Regional and Language Options_ in your windows machine. – Soner Gönül Feb 20 '14 at 07:17
  • I'm a bit inexperienced in this Culture stuff and I'm very sorry for asking this but where can I find that CultureInfo.CurrentCulture? Is it something I code somewhere in my project? or is it something i can find in the property window of my project? @SonerGönül.. Thanks for your help and I'll understand if you don't want to answer this question but i'll greatly appreciate it if you do. – Leonard Cupat Feb 20 '14 at 07:55
  • @LeonardCupat Have you stepped through the code in the debugger to see what the txt values are when that are parsed? and have you tried using `CultureInfo.InvariantCulture`? – D Stanley Feb 20 '14 at 13:54
  • @DStanley I have. When i the 'txtSellingPrice.text' shows the value that i have inputted. so as the – Leonard Cupat Feb 20 '14 at 15:58

4 Answers4

10

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);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • It is not working. I tried decimal.Parse and Convert.ToDecimal. I've done everything in your suggestion but it still produces the same error. Is it possible that the problem is coming from the textbox itself? or some datatype mismatch in the database that I'm using? – Leonard Cupat Feb 20 '14 at 04:57
4

I would give decimal.TryParse a go

decimal d;
if(decimal.TryParse(textbox.Text, out d))
{
//do something
}
matt_lethargic
  • 2,706
  • 1
  • 18
  • 33
1

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.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

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.

philn5d
  • 636
  • 7
  • 12