Home > other >  How do I show all decimal points?
How do I show all decimal points?

Time:12-10

I'm currently trying to do:

double keyTimes = 789789347928325 * Math.Pi
Console.WriteLine(keyTimes);

And it gives me: 2.4811964133351E 15

I need it to give me the full number of digits for a project, how can I achieve this? I want something like: 2.4811964133351468979725207509245720957294570275973459709345787430 etc.

CodePudding user response:

double can hold up to 17 digits, so you should use the decimal which is the floating-point numeric type with the highest precision (28-29 digits). Since the Math.PI constant is a double too you would need to define your own:

var pi = 3.14159265358979323846264338327m;
var keyTimes = 789789347928325 * pi;
// output 2481196413335099.0078141712097

To get better results you could try to find an external library. For example there is a BigDecimal nuget, but it hasn't been updated for long.

CodePudding user response:

Obtain as many digits of PI as you want, e.g.

https://github.com/eneko/Pi/blob/master/one-million.txt (one million)

https://stuff.mit.edu/afs/sipb/contrib/pi/ (one billion)

Turn PI into BigInteger, do arithmetics, restore the decimal point:

using System.Numerics;

...

string piStr = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";

BigInteger pi = BigInteger.Parse(piStr.Replace(".", ""));

BigInteger value = pi * 789789347928325;

string result = value.ToString().Insert(16, ".");

Console.WriteLine(result);

Output:

2481196413335099.0078141712096700227201776435541531843170785955041765369811708253654976044892515801198900164883582675

Fiddle

  • Related