4

The input string in textbox is, say, $10.00 . I call

decimal result;
var a = decimal.TryParse(text, NumberStyles.AllowCurrencySymbol, cultureInfo, out result);

cultureInfo is known (en-US). Why does decimal.tryParse returns false?

Thank you.

Igor S
  • 638
  • 7
  • 15

3 Answers3

10

The problem is you've allowed the currency symbol itself, but you've omitted other properties that are required to parse it correctly (decimal point, for example.) What you really want is NumberStyles.Currency:

decimal.TryParse("$10.00", NumberStyles.Currency, cultureInfo, out result);
dlev
  • 48,024
  • 5
  • 125
  • 132
2

Try this, you need to include NumberStyles.Number in the bitwise combination of values for the style argument:

decimal result;
var a = decimal.TryParse(text, NumberStyles.Number | NumberStyles.AllowCurrencySymbol, cultureInfo, out result);
greg84
  • 7,541
  • 3
  • 36
  • 45
1

You forgot to allow the decimal point, too:

decimal result;
var enUS = new System.Globalization.CultureInfo("en-US");
var a = decimal.TryParse("$10.00", System.Globalization.NumberStyles.AllowCurrencySymbol | System.Globalization.NumberStyles.AllowDecimalPoint , enUS, out result);

Console.WriteLine(enUS);
Console.WriteLine(a);
Console.WriteLine(result);
nvoigt
  • 75,013
  • 26
  • 93
  • 142