8

Until today I (implicitly) assumed that when I output a BigInteger via the parameterless (overridden) instance method BigInteger.ToString() then the return string would contain the complete and exact decimal representation of my "big" integer.

But on the MSDN doc page I read:

"In most cases, 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. However, BigInteger supports the "R" standard format specifier, which is intended to round-trip numeric values. The string returned by the ToString(String) method with the "R" format string preserves the whole BigInteger value and can then be parsed with the Parse or TryParse method to restore its original value without any loss of data"._

However, I have not been able to find an example where myBigInt.ToString() differs from myBigInt.ToString("R"). The Remarks example from the above page does not, as claimed, provide an example, in my version of the framework (.NET 4.0 SP1). (Also "G" and "R" seem to be equivalent for BigInteger.)

Is it safe to assume that the paramterless ToString() gives the full decimal expansion of the integer? Did they change framework behavior, or has the above mentioned example code always been wrong?

Note: SO shows me this "similar question" 2984184 after I wrote this one, but I'm still submitting my question because the answer of 2984184 does not really claryfy how the zero-parameter ToString() works.

EDIT BY MYSELF:

It was a bit incosequent of me to link the ToString(String) overload on MSDN when my question is (primarily) about the ToString() overload. So see also MSDN on BigInteger.ToString() which mentions the 50 digits limit as well but does not provide an example with more than 50 digits.

4

3 回答 3

5

Decompiling the .NET 4.0 framework gives this chain of calls:

BigInteger.ToString
BigNumber.FormatBigInteger( ... format:= null ...)
BigNumber.ParseFormatSpecifier(format ...)

within which we have

  if (string.IsNullOrEmpty(format))
    return 'R';

so it can be seen that in this implementation, ToString() is equivalent to ToString("R"). But, as pointed out by @PaulPhillips, given that the docs (slightly confusedly) explicitly disclaim the reliability of this, I would say the safest (and in any case the most self-documenting) thing to do is use "R" when you need full fidelity.

于 2012-07-05T14:10:47.560 回答
3

I do not think it is a safe assumption.

Since they have explicitly documented it as not doing that, it seems like a bad idea to rely on this behavior. If it actually is the same, then its undocumented and could theoretically change at any time (though this seems unlikely).

于 2012-07-05T13:58:57.470 回答
0

Looking at the source code the ToString() method simply calls:

BigNumber.FormatBigInteger(this, (string)null, NumberFormatInfo.CurrentInfo);

From there, the internal BigNumber.FormatBigInteger(BigInteger value, string format, NumberFormatInfo info) method has the following logic:

int digits = 0;
char format1 = BigNumber.ParseFormatSpecifier(format, out digits);

So, now we look at the BigNumber.ParseFormatSpecifier(string format, out int digits) method and discover that because it passed in a null string it simply returns the character 'R' with the number of digits (digits) equal to -1. Whereas, if you actually passed in a string format you can specify the number of digits as well, for example... 'R25' and it will parse out 25 as the number of digits.

This is what I understood by looking at the source code.

于 2012-07-05T14:19:31.547 回答