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

ConvertToPrecScale() public static method

public static ConvertToPrecScale ( SqlDecimal n, int precision, int scale ) : SqlDecimal
n SqlDecimal
precision int
scale int
return SqlDecimal
        public static SqlDecimal ConvertToPrecScale(SqlDecimal n, int precision, int scale)
        {
            CheckValidPrecScale(precision, scale);
            n.AssertValid();

            if (n.IsNull)
                return SqlDecimal.Null;

            SqlDecimal ret = n;

            int lPrecAdjust = precision - ret._bPrec;//Adjustment to precision
            int lScaleAdjust = scale - ret._bScale;//Adjustment to scale

            //Adjust scale
            ret.AdjustScale(lScaleAdjust, true);

            //The number of bytes storage required by the new precision
            byte cbWithNewPrec = CLenFromPrec((byte)precision);

            if (cbWithNewPrec < ret._bLen)
            {
                //if current actual length greater than length corresponding to bNewPrec
                //there must be truncating
                throw new SqlTruncateException();
            }
            else if (cbWithNewPrec == ret._bLen)
            {
                //if the two lengths equal, need to check the actual precision
                if (precision < ret.CalculatePrecision())
                    throw new SqlTruncateException();
            }

            //Adjust precision
            ret._bPrec = (byte)precision;

            ret.AssertValid();

            return ret;
        }