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
  • 94,086
  • 102
  • 195
  • 339
Sachin Prasad
  • 5,206
  • 12
  • 52
  • 98

2 Answers2

5

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
  • 94,086
  • 102
  • 195
  • 339
  • 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
  • 21,161
  • 8
  • 73
  • 75