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?
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...
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)
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 $.
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 :)