Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Convert Currency-formatted string back to decimal

Reply
Thread Tools

Convert Currency-formatted string back to decimal

 
 
KB
Guest
Posts: n/a
 
      10-01-2004
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}", 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


 
Reply With Quote
 
 
 
 
Mark Fitzpatrick
Guest
Posts: n/a
 
      10-01-2004
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

"KB" <(E-Mail Removed)> wrote in message
news:eo5%23L8%(E-Mail Removed)...
> 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}", 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
>
>



 
Reply With Quote
 
 
 
 
Alex Homer
Guest
Posts: n/a
 
      10-03-2004
Have you tried this:
http://msdn.microsoft.com/library/de...parsetopic.asp

It sounds like it should work...


 
Reply With Quote
 
Pabinator Pabinator is offline
Junior Member
Join Date: Aug 2009
Posts: 1
 
      08-28-2009
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 by Pabinator; 08-28-2009 at 02:30 AM..
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert from Decimal('1.23456789') to Decimal('1.234') valpa Python 11 03-24-2009 07:11 AM
Error: Cannot convert Decimal("0.0000") to Decimal Vitaliy Python 1 05-29-2008 10:36 AM
Convert currency formated string back into decimal =?Utf-8?B?R3JlZyBH?= ASP .Net 1 11-03-2007 04:48 PM
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM
Convert Currency-formatted string back to decimal Ritu.Chandra.JBS@gmail.com ASP .Net 2 05-04-2006 01:14 PM



Advertisments