Convert Currency-formatted string back to decimal

K

KB

Hi guys,

In my DataGrid I have a column that displays decimal values as currency ( I
set the Data Formatting expression of that column to {0:C}). So the actual
string displayed in the grid looks like $2,295.99.
For each row I need to do some additional calculations based on this value,
so I need to convert it back to decimal type. But I cannot find a way to get
rid of the currency formatting and revert the value back to decimal. I tried
the following:

Convert.ToDecimal( e.Item.Cells[ColValue].Text ); // Input string was
not in a correct format
String.Format( "{0:N}", e.Item.Cells[ColValue].Text) // returns $2,295.99
String.Format( "{0:D}", e.Item.Cells[ColValue].Text) // returns $2,295.99
String.Format( "{0:G}", e.Item.Cells[ColValue].Text) // returns $2,295.99
String.Format( "{0:R}", e.Item.Cells[ColValue].Text) // returns $2,295.99

Anyone knows how to solve this.

Cheers

Kevin
 
M

Mark Fitzpatrick

Kevin,
Just remove the $ and the , like so
e.Item.Cells[ColValue].Text.Replace("$","").Replace(",",""). The replace
just replaces them with nothing, essentially removing them from the string.

Hope this helps,
Mark FItzpatrick
Microsoft MVP - FrontPage
 

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

Top