Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2004

Tip: Looking for answers? Try searching our database.

An incredible bug in decimal.ToString()

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Simon Law - 20 Oct 2004 14:41 GMT
Hi,

This is unbelievable and totally the craziest thing I've ever seen.
Code:
  decimal d1=decimal.Parse("2");
  decimal d1=decimal.Parse("2.0");
  decimal d1=decimal.Parse("2.00");
  string s1=d1.ToString();
  string s2=d2.ToString();
  string s2=d3.ToString();
gives
  s1="2"
  s2="2.0"
  s3="2.00"

This happens under .NET framework 1.1, but not 1.0.

decimal.ToString() should return a string using "G" format.

Simon.
Robert Jordan - 20 Oct 2004 15:24 GMT
> Hi,
>
[quoted text clipped - 14 lines]
>
> decimal.ToString() should return a string using "G" format.

I cannot find any unbelievable and crazy things inside(TM).
.NET 1.0 ToString("G") doesn't return the fractional part
when it's zero. Never user the general format when you
expect compatibility with non-dotNet code.

bye
Rob
Jon Skeet [C# MVP] - 20 Oct 2004 15:36 GMT
> This is unbelievable and totally the craziest thing I've ever seen.
> Code:
[quoted text clipped - 10 lines]
>
> This happens under .NET framework 1.1, but not 1.0.

Yup.

> decimal.ToString() should return a string using "G" format.

And indeed it does. From the docs for standard numeric format strings,
the "G" specifier in particular:

<quote>
The exception to the preceding rule is if the number is a Decimal and
the precision specifier is omitted. In that case, fixed-point notation
is always used and trailing zeroes are preserved.
</quote>

This is all by design - the behaviour changed from 1.0 to 1.1 because
MS recognised that for some applications you really need to keep the
number of trailing zeroes.

See http://www.yoda.arachsys.com/csharp/decimal.html for a bit more on
this.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.