0

I am using C# inside .Net Environment. I have some really large integer values entered by user which are than divided by each other and the result is displayed. Now we know that 4/2 = 2 is simple but what if we divide

.0232321312321312312312312321032132139813912839013809123801283012983901283012380129382190381209382190382190382109382109382109312/ 1200000232323213213213213878978398494849023834902348239048239048239048239048239048239048239048239048239048239048239048239048239048239048239048239048239048234902384902384902384902348239048239048234

The answer is like 1.93601056119411E-197

I want to display the answer as a full decimal/floating point value instead of relying on E symbol.

Muhammad Ali
  • 72
  • 10
  • 1
    possible duplicate of [Remove E sign from big float number , C# ?](http://stackoverflow.com/questions/4423054/remove-e-sign-from-big-float-number-c) –  Apr 13 '11 at 13:02
  • dividing this as is, gives out integral is too large error, where as dividing by 1.29... gives the result of 0.0193601056119411. So I'm not sure what... – iamserious Apr 13 '11 at 13:03
  • I'm pretty sure Jon Skeet has some code to do this. Now if only he were around somewhere... – Gabe Apr 13 '11 at 13:53

3 Answers3

2

I wrote some code

using System.Numerics;

BigInteger x = BigInteger.Parse("0232321312321312312312312321032132139813912839013809123801283012983901283012380129382190381209382190382190382109382109382109312");
BigInteger y = BigInteger.Parse("1200000232323213213213213878978398494849023834902348239048239048239048239048239048239048239048239048239048239048239048239048239048239048239048239048239048234902384902384902384902348239048239048234
")
BigInteger answer = BigInteger.Divide(x, y)
return answer.ToString("D")

I think that is what you are looking for, if you want the fractional part of the answer you'll have to do it all with the Complex type. Its not clear what you are doing from the question.

EDIT:

How about this

using System.Numerics;

BigInteger x = BigInteger.Parse("0232321312321312312312312321032132139813912839013809123801283012983901283012380129382190381209382190382190382109382109382109312");
BigInteger y = BigInteger.Parse("1200000232323213213213213878978398494849023834902348239048239048239048239048239048239048239048239048239048239048239048239048239048239048239048239048239048234902384902384902384902348239048239048234
")
Complex answer = Complex.Divide((Complex)x, (Complex)y)
return answer.ToString("F50") //If 50 decimal places is enough.
Jodrell
  • 34,946
  • 5
  • 87
  • 124
1

I use .ToString("N0") that seems to do the job when I have the same problem.

JustABitOfCode
  • 984
  • 1
  • 7
  • 12
  • Sorry, I misread the question. .ToString("N0") will format the number with 0 decimal places. The problem I was facing was formatting a number with a +ve power, not a -ve one. – JustABitOfCode Apr 13 '11 at 13:07
  • Yes, it will. Because your result has a -ve power, all the interesting digits are behind the decimal point, so formatting the number with 0 decimal places will return 0. Try .ToString("N50"). I think that should return the number with the first 50 decimal places, although I haven't tested that. – JustABitOfCode Apr 13 '11 at 13:15
1

I assume that you not only want to preceed your value with "0.00..." but also want to see the "missing" digits after 411.

If that is the case, then you cannot do this with floating point data types, since the floating point data types in .NET simply do not have the required precision. You will need to resort to external libraries, such as, for example, W3b.Sine (untested, just found it through a quick Google search), which allow for arbitrary-precision decimals.

Heinzi
  • 167,459
  • 57
  • 363
  • 519