Results 1 to 5 of 5

Thread: Decimal.ToString("C") ---> back to decimal?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    64

    Decimal.ToString("C") ---> back to decimal?

    So in my application I have a decimal value that I convert to a currency string using Decimal.ToString("C"). This produces something visually appealing like $1.25 or ($1.25)

    My problem is, at some point I need to go back from the text value to a decimal value. Is there an easy way of doing this aside from manually picking the non-numeric characters off and then converting?

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Decimal.ToString("C") ---> back to decimal?

    Can you just keep the value in a variable, convert to a string when needed, and keep that value in the variable? That way when you need the decimal again, you still have the value there. Otherwise, I would have done what you have suggested, take off the non numeric characters...

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Decimal.ToString("C") ---> back to decimal?

    VB Code:
    1. Dim decString As String = 1.25D.ToString("C")
    2.         Dim dec As Decimal = Decimal.Parse(decString.TrimStart("$"c))
    3.         MessageBox.Show("I'm a string " & decString & " and I'm a decimal " & dec.ToString)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decimal.ToString("C") ---> back to decimal?

    You should absolutely take gigemboy's advice if at all possible. If that's not possible then wild_bill is on the right track, except that Decimal.Parse is an overloaded method:
    VB Code:
    1. Dim decString As String = 1.25D.ToString("C")
    2.         Dim dec As Decimal = Decimal.Parse(decString, Globalization.NumberStyles.Currency)
    3.         MessageBox.Show("I'm a string " & decString & " and I'm a decimal " & dec.ToString)
    That way, just like converting to a currency, it will use the local currency format and not just assume you're in the US, or somewhere else that uses $.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    64

    Re: Decimal.ToString("C") ---> back to decimal?

    Well I'm actually sorting a listview by a column that is formatted as a currency so the value is a text value. I'm just going to store the decimal value in the listview along with the string. Easy solution

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width