开发者

C# - How To Convert/Display a Floating-Point/Exponential Result into a Full/Whole Number

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

.02323213123213123123123123210321321398139128390138091238012830129839012830123801293821903812093821开发者_开发百科90382190382109382109382109312/ 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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜