views:

75

answers:

1

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?

+3  A: 

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"
Nick Craver
formatting using "G" { v.ToString("G") } gives the same 60 significant digits output. What are the formats that you only get 50 digits for?
brickner
@brickner: I believe only `g` and `r` will return the full original value for any length, but I'm not 100% sure, rarely use numbers this large in our projects.
Nick Craver