|
-
Dec 14th, 2005, 09:08 PM
#1
Thread Starter
Lively Member
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?
-
Dec 14th, 2005, 09:14 PM
#2
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...
-
Dec 14th, 2005, 09:37 PM
#3
Re: Decimal.ToString("C") ---> back to decimal?
VB Code:
Dim decString As String = 1.25D.ToString("C")
Dim dec As Decimal = Decimal.Parse(decString.TrimStart("$"c))
MessageBox.Show("I'm a string " & decString & " and I'm a decimal " & dec.ToString)
-
Dec 15th, 2005, 12:50 AM
#4
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:
Dim decString As String = 1.25D.ToString("C")
Dim dec As Decimal = Decimal.Parse(decString, Globalization.NumberStyles.Currency)
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 $.
-
Dec 15th, 2005, 07:27 AM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|