Decimal.Parse and Decimal.ToString Methods

M

Mythran

Dim value As Decimal = 1234.56
Dim format As String = _
"Positive $#,##0.00;Negative $#,##0.00;"

Dim s As String = value.ToString(format)
Dim d As Decimal = Decimal.Parse(s)

The last line above throws an exception because it can't parse "Positive
$1,234.56". It can convert to that format, but how can one go about parsing
it using the same format that was used to format it as a string? Is there
an easy way w/o too much work?

Thanks,
Mythran
 
G

Guest

Mythran,

There is probably a more elegant way, but this might work for you:

Dim value As Decimal = 1234.56
Dim s As String = value.ToString("Negative $#,##0.00;Negative
$#,##0.00;")
Dim s2 As String
Dim d As Decimal

s2 = s.Replace("Positive ", "")
s2 = s2.Replace("Negative ", "-")
d = Decimal.Parse(s2, System.Globalization.NumberStyles.Currency)

MsgBox(s)
MsgBox(d)

Kerry Moorman
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top