Пример #1
0
        /// <summary>
        /// Constructs a new MathContext with the specified precision and with the specified rounding mode.
        /// <para>If the precision passed is zero, then this implies that the computations have to be performed exact, the rounding mode in this case is irrelevant.</para>
        /// </summary>
        ///
        /// <param name="Precision">The precision for the new MathContext</param>
        /// <param name="RoundingMode">The rounding mode for the new MathContext</param>
        ///
        /// <exception cref="ArgumentException">Thrown if the Precision is less than zero</exception>
        public MathContext(int Precision, RoundingModes RoundingMode)
        {
            if (Precision < 0)
            {
                throw new ArgumentException("Digits < 0!");
            }

            this.m_precision    = Precision;
            this.m_roundingMode = RoundingMode;
        }
Пример #2
0
 public static double Round(double value, int digits, MidpointRounding mode) =>
 RoundingModes.Round(value, digits, mode);
Пример #3
0
 public static float Round(float value, int digits, MidpointRounding mode) =>
 RoundingModes.Round(value, digits, mode);
Пример #4
0
        /// <summary>
        /// Constructs a new MathContext from a string.
        /// <para>The string has to specify the precision and the rounding mode to be used and has to follow the following syntax:
        /// "Precision=&lt;Precision&gt; RoundingMode=&lt;RoundingMode&gt;"
        /// This is the same form as the one returned by the ToString method.</para>
        /// </summary>
        ///
        /// <param name="Value">A string describing the precision and rounding mode for the new MathContext</param>
        ///
        /// <exception cref="ArgumentException">Thrown if the string is not in the correct format or if the Precision specified is &lt; 0</exception>
        public MathContext(String Value)
        {
            char[] charVal = Value.ToCharArray();
            int    i;     // Index of charVal
            int    j;     // Index of chRoundingMode
            int    digit; // It will contain the digit parsed

            if ((charVal.Length < 27) || (charVal.Length > 45))
            {
                throw new ArgumentException("Bad string format!");
            }

            // Parsing "precision=" String
            for (i = 0; (i < m_chPrecision.Length) && (charVal[i] == m_chPrecision[i]); i++)
            {
                ;
            }

            if (i < m_chPrecision.Length)
            {
                throw new ArgumentException("bad string format!");
            }

            // Parsing the value for "precision="...
            digit = CharUtils.ToDigit(charVal[i], 10);

            if (digit == -1)
            {
                throw new ArgumentException("bad string format!");
            }

            this.m_precision = this.m_precision * 10 + digit;
            i++;

            do
            {
                digit = CharUtils.ToDigit(charVal[i], 10);
                if (digit == -1)
                {
                    if (charVal[i] == ' ')
                    {
                        // It parsed all the digits
                        i++;
                        break;
                    }
                    // It isn't  a valid digit, and isn't a white space
                    throw new ArgumentException("Bad string format!");
                }
                // Accumulating the value parsed
                this.m_precision = this.m_precision * 10 + digit;

                if (this.m_precision < 0)
                {
                    throw new ArgumentException("Bad string format!");
                }

                i++;
            } while (true);
            // Parsing "roundingMode="
            for (j = 0; (j < m_chRoundingMode.Length) && (charVal[i] == m_chRoundingMode[j]); i++, j++)
            {
                ;
            }

            if (j < m_chRoundingMode.Length)
            {
                throw new ArgumentException("Bad string format!");
            }
            // Parsing the value for "roundingMode"...
            this.m_roundingMode = (RoundingModes)Enum.Parse(typeof(RoundingModes), new string(charVal, i, charVal.Length - i), true);
        }
Пример #5
0
        /// <summary>
        /// Constructs a new MathContext with the specified precision and with the specified rounding mode.
        /// <para>If the precision passed is zero, then this implies that the computations have to be performed exact, the rounding mode in this case is irrelevant.</para>
        /// </summary>
        /// 
        /// <param name="Precision">The precision for the new MathContext</param>
        /// <param name="RoundingMode">The rounding mode for the new MathContext</param>
        /// 
        /// <exception cref="ArgumentException">Thrown if the Precision is less than zero</exception>
        public MathContext(int Precision, RoundingModes RoundingMode)
        {
            if (Precision < 0)
                throw new ArgumentException("Digits < 0!");

            this._precision = Precision;
            this._roundingMode = RoundingMode;
        }
Пример #6
0
        /// <summary>
        /// Constructs a new MathContext from a string.
        /// <para>The string has to specify the precision and the rounding mode to be used and has to follow the following syntax:
        /// "Precision=&lt;Precision&gt; RoundingMode=&lt;RoundingMode&gt;"
        /// This is the same form as the one returned by the ToString method.</para>
        /// </summary>
        /// 
        /// <param name="Value">A string describing the precision and rounding mode for the new MathContext</param>
        /// 
        /// <exception cref="ArgumentException">Thrown if the string is not in the correct format or if the Precision specified is &lt; 0</exception>
        public MathContext(String Value)
        {
            char[] charVal = Value.ToCharArray();
            int i; // Index of charVal
            int j; // Index of chRoundingMode
            int digit; // It will contain the digit parsed

            if ((charVal.Length < 27) || (charVal.Length > 45))
                throw new ArgumentException("Bad string format!");
            
            // Parsing "precision=" String
            for (i = 0; (i < _chPrecision.Length) && (charVal[i] == _chPrecision[i]); i++)
            {
                ;
            }

            if (i < _chPrecision.Length)
                throw new ArgumentException("bad string format!");
            
            // Parsing the value for "precision="...
            digit = CharUtils.ToDigit(charVal[i], 10);

            if (digit == -1)
                throw new ArgumentException("bad string format!");
            
            this._precision = this._precision * 10 + digit;
            i++;

            do
            {
                digit = CharUtils.ToDigit(charVal[i], 10);
                if (digit == -1)
                {
                    if (charVal[i] == ' ')
                    {
                        // It parsed all the digits
                        i++;
                        break;
                    }
                    // It isn't  a valid digit, and isn't a white space
                    throw new ArgumentException("Bad string format!");
                }
                // Accumulating the value parsed
                this._precision = this._precision * 10 + digit;

                if (this._precision < 0)
                    throw new ArgumentException("Bad string format!");
                
                i++;
            } while (true);
            // Parsing "roundingMode="
            for (j = 0; (j < _chRoundingMode.Length) && (charVal[i] == _chRoundingMode[j]); i++, j++)
            {
                ;
            }

            if (j < _chRoundingMode.Length)
                throw new ArgumentException("Bad string format!");
            // Parsing the value for "roundingMode"...
            this._roundingMode = (RoundingModes)Enum.Parse(typeof(RoundingModes), new string(charVal, i, charVal.Length - i), true);
        }