Icemanind
Icemanind

Reputation: 48686

Parsing a decimal number

Guys, I have a string that contains a decimal number. The problem is, sometimes it is negative and it is stored in accounting format (positive number surrounded by parenthesis). In other words, I got a string like this:

string s = "(35.00)";

What I'm doing currently is:

decimal TheValue = decimal.Parse(s);

This value of TheValue should be -35.00. It apparently doesn't know what the parenthesis mean, so its just storing 0 in Thevalue. Anyone know how to make the decimal.Parse() function look for parenthesis?

Upvotes: 2

Views: 638

Answers (1)

Tim Robinson
Tim Robinson

Reputation: 54734

Take a look at the decimal.Parse overload that accepts a NumberStyles enum. Specifically, you'll need to include NumberStyles.AllowParentheses.

Upvotes: 17

Related Questions