Unformatting formatted strings?

  • Thread starter Thread starter SamIAm
  • Start date Start date
S

SamIAm

I am displaying money values in a textbox like this:
txtCountryPack.Text = String.Format("{0:c}", dr[0]["Pack"]);

I then need to updated my Database:
decimal packPrice = Convert.ToDecimal(txtCountryPack.Text.Trim());

The problem is that the value is in a formatted format i.e. $300.00
How do I remove the formatting with have to do a SubString?

Cheers.

S
 
SamIAm,
Consider using Decimal.Parse directly instead, Convert.ToDecimal indirectly
calls Decimal.Parse.

There is an overloaded version that accepts the Globalization.NumberStyles
enum.

There is a NumberStyles.Currency value, which will parse out the numeric
dollar amount.

decimal packPrice = Decimal.Parse(txtCountryPack.Text.Trim(),
NumberStyles.Currency);

The other option is to use the overloaded Convert.ToDecimal that accepts a
format provider, if there is a format provider that is a Currency amount.

Hope this helps
Jay
 
Thanks - works like a charm

S


Jay B. Harlow said:
SamIAm,
Consider using Decimal.Parse directly instead, Convert.ToDecimal indirectly
calls Decimal.Parse.

There is an overloaded version that accepts the Globalization.NumberStyles
enum.

There is a NumberStyles.Currency value, which will parse out the numeric
dollar amount.

decimal packPrice = Decimal.Parse(txtCountryPack.Text.Trim(),
NumberStyles.Currency);

The other option is to use the overloaded Convert.ToDecimal that accepts a
format provider, if there is a format provider that is a Currency amount.

Hope this helps
Jay

SamIAm said:
I am displaying money values in a textbox like this:
txtCountryPack.Text = String.Format("{0:c}", dr[0]["Pack"]);

I then need to updated my Database:
decimal packPrice = Convert.ToDecimal(txtCountryPack.Text.Trim());

The problem is that the value is in a formatted format i.e. $300.00
How do I remove the formatting with have to do a SubString?

Cheers.

S
 
Back
Top