BigInteger.ToString() returns more than 50 decimal digits

本秂侑毒 提交于 2019-12-24 16:22:49

问题


I'm using .NET 4 System.Numerics.BigInteger Structure and I'm getting results different from the documentation.

In the documentation of BigInteger.ToString() Method It says:

The ToString() method supports 50 decimal digits of precision. That is, if the BigInteger value has more than 50 digits, only the 50 most significant digits are preserved in the output string; all other digits are replaced with zeros.

I have some code that takes a 60 decimal digits BigInteger and converts it to a string. The 60 significant decimal digits string didn't lose any significant digits:

const string vString = "123456789012345678901234567890123456789012345678901234567890";
Assert.AreEqual(60, vString.Length);
BigInteger v = BigInteger.Parse(vString);
Assert.AreEqual(60, v.ToString().Length);
Assert.AreEqual('9', v.ToString()[58]);
Assert.AreEqual('1', v.ToString()[0]);
Assert.AreEqual(vString, v.ToString());
Assert.AreEqual(vString, v.ToString("G"));

All the asserts pass.

What exactly does the quoted part of the documentation mean?


回答1:


The documentation is a little unclear here, this limit only applies when formatting the string, for example:

v.ToString("0"); "123456789012345678901234567890123456789012345678900000000000"
v.ToString("n0"); "123,456,789,012,345,678,901,234,567,890,123,456,789,012,345,678,900,000,000,000"

The exception is formatting it as "R", which gives the original round-tripped value:

v.ToString("R"); "123456789012345678901234567890123456789012345678901234567891"


来源:https://stackoverflow.com/questions/2984184/biginteger-tostring-returns-more-than-50-decimal-digits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!