4

I am trying to parse or convert a string to decimal in C#.

I need to be able to parse strings like,

$123,345,676.8999 to its equivalent 123345676.90.

I need to have only 2 places after the decimal point and that needs to be suitably rounded.

Can you guys please suggest a way to do the above? Basically, any string in the form of currency (with $,pound symbol etc). I should be able to parse it and convert it to decimal.

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
Tendulkar
  • 43
  • 1
  • 1
  • 3

3 Answers3

15

Try this:

var val = double.Parse("$123,345,676.8999", NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint | NumberStyles.AllowCurrencySymbol);
val = Math.Round(val, 2);
Chandu
  • 81,493
  • 19
  • 133
  • 134
4

You can use decimal.TryParse to perform the parsing -- the link has some samples on how the parsing works.

Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
0

Very simple answer: use Decimal.Parse()

Jakub
  • 534
  • 3
  • 17