System.Data.SqlTypes.SqlDecimal.ToString C# (CSharp) Method

ToString() public method

public ToString ( ) : string
return string
        public override string ToString()
        {
            if (IsNull)
                return SQLResource.s_nullString;
            AssertValid();

            // Make local copy of data to avoid modifying input.
            uint[] rgulNumeric = new uint[4] { _data1, _data2, _data3, _data4 };
            int culLen = _bLen;
            char[] pszTmp = new char[s_NUMERIC_MAX_PRECISION + 1];   //Local Character buffer to hold
                                                                     //the decimal digits, from the
                                                                     //lowest significant to highest significant

            int iDigits = 0;//Number of significant digits
            uint ulRem; //Remainder of a division by x_ulBase10, i.e.,least significant digit

            // Build the final numeric string by inserting the sign, reversing
            // the order and inserting the decimal number at the correct position

            //Retrieve each digit from the lowest significant digit
            while (culLen > 1 || rgulNumeric[0] != 0)
            {
                MpDiv1(rgulNumeric, ref culLen, s_ulBase10, out ulRem);
                //modulo x_ulBase10 is the lowest significant digit
                pszTmp[iDigits++] = ChFromDigit(ulRem);
            }

            // if scale of the number has not been
            // reached pad remaining number with zeros.
            while (iDigits <= _bScale)
            {
                pszTmp[iDigits++] = ChFromDigit(0);
            }


            int uiResultLen = 0;
            int iCurChar = 0;
            char[] szResult;

            // Increment the result length if scale > 0 (need to add '.')            
            if (_bScale > 0)
            {
                uiResultLen = 1;
            }

            if (IsPositive)
            {
                szResult = new char[uiResultLen + iDigits];
            }
            else
            {
                // Increment the result length if negative (need to add '-')            
                szResult = new char[uiResultLen + iDigits + 1];
                szResult[iCurChar++] = '-';
            }

            while (iDigits > 0)
            {
                if (iDigits-- == _bScale)
                    szResult[iCurChar++] = '.';

                szResult[iCurChar++] = pszTmp[iDigits];
            }

            AssertValid();

            return new string(szResult);
        }

Usage Example

        private static Decimal ToDecimal(SqlDecimal value)
        {
            var data = value.Data;
            var scale = value.Scale;

            if (data[3] != 0 || scale > 28)
            {
                var result = decimal.Parse(value.ToString());

                return result;
            }

            return new Decimal(data[0], data[1], data[2], !value.IsPositive, scale);
        }
All Usage Examples Of System.Data.SqlTypes.SqlDecimal::ToString