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
 
Joined
Aug 28, 2009
Messages
1
Reaction score
0
How to convert currency back to no currency decimal

Just for the records:

public decimal strCurrencyToDecimal(string Value)
{
if (Value.Length == 0)
return 0;
else
return Decimal.Parse(Value.Replace(" ", ""), NumberStyles.Any | NumberStyles.AllowCurrencySymbol | NumberStyles.AllowThousands
| NumberStyles.AllowDecimalPoint);
}


Pablo
 
Last edited:

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top