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 / July 2005

Tip: Looking for answers? Try searching our database.

Displaying Decimals as a string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
michael_quinlivan@hotmail.com - 01 Jul 2005 03:29 GMT
Hi All

I have the fraction 256139/25631, which I have stored in a Decimal data
types, due to it having the highest precision.  When I display this
using Console.Writeline, it only displays a precision of 15 digits
(9.99...).  I know that Decimals hold higher precisions than this, but
I don't know how I can get it to display all digits.  It seems to
always round the value.  I have tried using NumberFormatInfo classes
and all sorts of tricks, but nothing seems to work.  Any ideas?

cheers
MQ
Jon Skeet [C# MVP] - 01 Jul 2005 06:58 GMT
> I have the fraction 256139/25631, which I have stored in a Decimal data
> types, due to it having the highest precision.

My guess is that you're doing the division using doubles, and then
converting the result to a decimal - in which case you've lost the
extra precision already.

The following program shows the difference - I suspect you're doing the
second thing, when you should be doing the first:

using System;

public class Test
{
   static void Main()
   {
       Decimal d = 256139m/25631m;
       Console.WriteLine (d);
       
       d = (decimal)(256139d/25631);
       Console.WriteLine (d);
   }
}

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.