469,917 Members | 1,713 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

Decimal.Parse and Decimal.ToString Methods

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
Nov 21 '05 #1
1 7310
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
"Mythran" wrote:
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

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mark | last post: by
2 posts views Thread by Piotr | last post: by
3 posts views Thread by JenHu | last post: by
3 posts views Thread by Stefan Richter | last post: by
4 posts views Thread by =?Utf-8?B?cGF0cmlja2RyZA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.