decimal.Parse and negative numbers:

  • Thread starter Thread starter probashi
  • Start date Start date
P

probashi

Hi,

I am trying to the following:

String s = "( 54.05)";
decimal d = decimal.Parse(s);


when s = "( 54.05)" I what the value -54.05
and s = " 54.05" I what the value 54.05


Any suggestion.

Thanks.
 
Use the default formatting that Decimal expects, or specify the formatting that you are using:

Decimal.Parse(myString, System.Globalization.NumberStyles.Currency)
 
Decimal.Parse(myString, System.Globalization.NumberStyles.Currency)

is throwing Format Exception.
 
Than choose the correct style that matches your input from the System.Globalization.NumberStyles enumeration.

If the enumeration does not support your needs, you'll have to parse the string manually. In this case, I suggest using a Regex.
 
Back
Top