locked
BigInteger Class RRS feed

  • Question

  • Here is my code.

    BigInteger

     

     

    b1 = BigInteger.Parse("23098230498230983409822324234234897657821687324872335523234634463445");

     

     

    Console.WriteLine(b1.ToString("F2"));

    This outputs :

    23098230498230983409822324234234897657821687324872000000000000000000.00

    can someone tell me why its cutting it off and replacing with zeros.

    Tuesday, August 25, 2009 7:11 PM

Answers

  • From the documentation of BigInteger.ToString :

    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. The following example illustrates that a string output
    using the "R" format string can then be parsed by the Parse method without any data loss.

    If you want to see the full precision in BigInteger, I recommend sticking to using:

    Console.WriteLine(b1.ToString("R"));


    Reed Copsey, Jr. - http://reedcopsey.com
    Tuesday, August 25, 2009 7:19 PM

All replies

  • From the documentation of BigInteger.ToString :

    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. The following example illustrates that a string output
    using the "R" format string can then be parsed by the Parse method without any data loss.

    If you want to see the full precision in BigInteger, I recommend sticking to using:

    Console.WriteLine(b1.ToString("R"));


    Reed Copsey, Jr. - http://reedcopsey.com
    Tuesday, August 25, 2009 7:19 PM
  • Thank you.
    Tuesday, August 25, 2009 7:24 PM