Convert Currency-formatted string back to decimal

Discussion in 'ASP .Net' started by KB, Oct 1, 2004.

  1. KB

    KB Guest

    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
     
    KB, Oct 1, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:eo5%23L8%...
    > 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
    >
    >
     
    Mark Fitzpatrick, Oct 1, 2004
    #2
    1. Advertising

  3. KB

    Alex Homer Guest

    Alex Homer, Oct 3, 2004
    #3
  4. KB

    Pabinator

    Joined:
    Aug 28, 2009
    Messages:
    1
    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: Aug 28, 2009
    Pabinator, Aug 28, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martyn Fewtrell

    String (Currency) to Decimal Conversion

    Martyn Fewtrell, Jun 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    4,173
    Martyn Fewtrell
    Jun 21, 2005
  2. Replies:
    2
    Views:
    505
  3. =?Utf-8?B?R3JlZyBH?=

    Convert currency formated string back into decimal

    =?Utf-8?B?R3JlZyBH?=, Nov 3, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    911
    Martin Honnen
    Nov 3, 2007
  4. Vitaliy
    Replies:
    1
    Views:
    489
    Peter Otten
    May 29, 2008
  5. jason

    Re-convert Currency back to Integer?

    jason, Sep 1, 2003, in forum: ASP General
    Replies:
    1
    Views:
    178
    jason
    Sep 1, 2003
Loading...

Share This Page