PC Review


Reply
Thread Tools Rate Thread

Decimal.Parse and Decimal.ToString Methods

 
 
Mythran
Guest
Posts: n/a
 
      16th Aug 2005
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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?S2VycnkgTW9vcm1hbg==?=
Guest
Posts: n/a
 
      17th Aug 2005
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
>
>
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird CultureInfo - DateTime.Parse() and Decimal.Parse() Rico Microsoft C# .NET 8 22nd Sep 2006 10:59 AM
Odd behaviour with Decimal.ToString Oenone Microsoft VB .NET 7 9th Nov 2005 02:52 PM
Decimal.ToString Method David Dvali Microsoft C# .NET 2 3rd Oct 2005 11:30 AM
An incredible bug in decimal.ToString() Simon Law Microsoft Dot NET Framework 2 20th Oct 2004 03:36 PM
decimal.ToString behaviour =?Utf-8?B?UE0=?= Microsoft Dot NET Framework 11 23rd May 2004 02:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:30 PM.