maximum length of Decimal.ToString(InvariantCulture) RRS feed

  • Question

  • I need to send System.Decimal values to another computer as strings. I intend to use Decimal.ToString(IFormatProvider) and specify CultureInfo.InvariantCulture so that the other computer will be able to parse the data regardless of culture settings. What is the longest possible length of the resulting string?

    • new System.Decimal(1, 0, 0, true, 28) results in "-0.0000000000000000000000000001".
    • new System.Decimal(-1, -1, -1, true, 28) results in "-7.9228162514264337593543950335".
    Both of these are 31 characters long, and I don't see how to get anything longer, so I think the answer is 31. Can someone confirm this or point out a mistake?
    Wednesday, September 10, 2014 2:14 PM


  • A Decimal field can store values between -79228162514264337593543950335 (Decimal.MinValue) and 79228162514264337593543950335 (Decimal.MaxValue) and if you add a decimal point to the min value, the length of the string becomes 31 so you are right.

          string s = Decimal.MinValue.ToString();
          int length = s.Length; //=30

    • Marked as answer by ranta Thursday, September 11, 2014 4:17 PM
    Wednesday, September 10, 2014 4:04 PM