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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top