1

What will be best way to get decimal value from $232,680.00 Is it possible without regular expression.

Also this does not work -

decimal.TryParse(Response.assess_market_value, NumberStyles.Currency, CultureInfo.InvariantCulture, out marketTotalValue);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sachin Prasad
  • 5,365
  • 12
  • 54
  • 101

2 Answers2

6

You can combine AllowCurrencySymbol, AllowDecimalPoint and AllowThousands styles and use a culture that has $ as a CurrencySymbol like en-US

var s = "$232,680.00";
decimal d = decimal.Parse(s, NumberStyles.AllowCurrencySymbol |
                             NumberStyles.AllowDecimalPoint | 
                             NumberStyles.AllowThousands, 
                             CultureInfo.GetCultureInfo("en-US"));

or more simple use NumberStyles.Currency instead which contains those styles as well.

decimal d = decimal.Parse(s, NumberStyles.Currency, CultureInfo.GetCultureInfo("en-US"));

The problem in your code is InvariantCulture has ¤ (which is Currency Sign (U+00A4)) not $ as a CurrencySymbol. If you change your InvariantCulture to CultureInfo.GetCultureInfo("en-US"), your code will work as well.

en-US culture totally fits for your string since it has . as a decimal separator and , as a thousand separator.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Using `NumberStyles.Currency` (instead of `NumberStyles.AllowDecimalPoint` and `NumberStyles.AllowThousands`) in combination with `NumberStyles.AllowCurrencySymbol` is arguably cleaner. – Rik Mar 04 '16 at 09:59
  • 1
    @Rik `Currency` _already_ contains `AllowCurrencySymbol` so it will be redundant to use it. Just using `Currency` is enough. – Soner Gönül Mar 04 '16 at 10:15
1

Have about:

decimal.Parse("$232,680.00", NumberStyles.Currency, CultureInfo.GetCultureInfo("en-US"))
Saeb Amini
  • 23,054
  • 9
  • 78
  • 76