2

I'm having issue converting this string to a decimal. I tried to follow the documentation here with no luck: Decimal.TryParse Method

string stringVal = "-(3434343434.00)";

NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands |
    NumberStyles.AllowParentheses | NumberStyles.AllowLeadingSign;

CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");

decimal value;
bool parseSuccess = decimal.TryParse(stringVal, style, culture, out value);

parseSuccess is returning false.

mservidio
  • 12,817
  • 9
  • 58
  • 84

2 Answers2

4

I think a - sign and parenthesis at the same time is not valid. -3434343434.00 is fine as is (3434343434.00) but -(3434343434.00) is not valid. -(3434343434.00) does not really make sense it is using 2 different methods to indicate a negative and as such is a bit redundant.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
4

You have both () and a - sign in your string. This is incorrect and is why your string doesn't parse.

See the documentations for AllowParentheses:

AllowParentheses: Indicates that the numeric string can have one pair of parentheses enclosing the number. The parentheses indicate that the string to be parsed represents a negative number.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Thanks! The creation of the sign and the parenthesis I now realize is a bug in Telerik's RadMaskedEditBox control, which is where I was getting the string. – mservidio Nov 16 '11 at 21:22