Displaying Decimals as a string

  • Thread starter Thread starter michael_quinlivan
  • Start date Start date
M

michael_quinlivan

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
 
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);
}
}
 
Back
Top