Пример #1
0
 /*
  * Constructs a new {@code MathContext} with the specified precision and
  * with the specified rounding mode. 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.
  *
  * @param precision
  *            the precision for the new {@code MathContext}.
  * @param roundingMode
  *            the rounding mode for the new {@code MathContext}.
  * @throws IllegalArgumentException
  *             if {@code precision < 0}.
  * @throws NullPointerException
  *             if {@code roundingMode} is {@code null}.
  */
 public MathContext(int precision, RoundingMode roundingMode)
 {
     if (precision < 0)
     {
         // math.0C=Digits < 0
         throw new java.lang.IllegalArgumentException("Digits < 0"); //$NON-NLS-1$
     }
     if (roundingMode == null)
     {
         // math.0D=null RoundingMode
         throw new java.lang.NullPointerException("null RoundingMode"); //$NON-NLS-1$
     }
     this.precision    = precision;
     this.roundingMode = roundingMode;
 }
Пример #2
0
        /*
         * Constructs a new {@code MathContext} from a string. 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 {@link #toString}
         * method.
         *
         * @param val
         *            a string describing the precision and rounding mode for the
         *            new {@code MathContext}.
         * @throws IllegalArgumentException
         *             if the string is not in the correct format or if the
         *             precision specified is &lt; 0.
         */
        public MathContext(String val)
        {
            char[] charVal = val.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))
            {
                // math.0E=bad string format
                throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
            }
            // Parsing "precision=" String
            for (i = 0; (i < chPrecision.Length) && (charVal[i] == chPrecision[i]); i++)
            {
                ;
            }

            if (i < chPrecision.Length)
            {
                // math.0E=bad string format
                throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
            }
            // Parsing the value for "precision="...
            digit = java.lang.Character.digit(charVal[i], 10);
            if (digit == -1)
            {
                // math.0E=bad string format
                throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
            }
            this.precision = this.precision * 10 + digit;
            i++;

            do
            {
                digit = java.lang.Character.digit(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
                    // math.0E=bad string format
                    throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
                }
                // Accumulating the value parsed
                this.precision = this.precision * 10 + digit;
                if (this.precision < 0)
                {
                    // math.0E=bad string format
                    throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
                }
                i++;
            } while (true);
            // Parsing "roundingMode="
            for (j = 0; (j < chRoundingMode.Length) &&
                 (charVal[i] == chRoundingMode[j]); i++, j++)
            {
                ;
            }

            if (j < chRoundingMode.Length)
            {
                // math.0E=bad string format
                throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
            }
            // Parsing the value for "roundingMode"...
            this.roundingMode = RoundingMode.valueOf(java.lang.StringJ.valueOf(charVal, i, charVal.Length - i).ToString());
        }
Пример #3
0
        /**
         * Constructs a new {@code MathContext} from a string. 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 {@link #toString}
         * method.
         *
         * @param val
         *            a string describing the precision and rounding mode for the
         *            new {@code MathContext}.
         * @throws IllegalArgumentException
         *             if the string is not in the correct format or if the
         *             precision specified is &lt; 0.
         */
        public MathContext(String val)
        {
            char[] charVal = val.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)) {
                // math.0E=bad string format
                throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
            }
            // Parsing "precision=" String
            for (i = 0; (i < chPrecision.Length) && (charVal[i] == chPrecision[i]); i++) {
                ;
            }

            if (i < chPrecision.Length) {
                // math.0E=bad string format
                throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
            }
            // Parsing the value for "precision="...
            digit = java.lang.Character.digit(charVal[i], 10);
            if (digit == -1) {
                // math.0E=bad string format
                throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
            }
            this.precision = this.precision * 10 + digit;
            i++;

            do {
                digit = java.lang.Character.digit(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
                    // math.0E=bad string format
                    throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
                }
                // Accumulating the value parsed
                this.precision = this.precision * 10 + digit;
                if (this.precision < 0) {
                    // math.0E=bad string format
                    throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
                }
                i++;
            } while (true);
            // Parsing "roundingMode="
            for (j = 0; (j < chRoundingMode.Length)
                    && (charVal[i] == chRoundingMode[j]); i++, j++) {
                ;
            }

            if (j < chRoundingMode.Length) {
                // math.0E=bad string format
                throw new java.lang.IllegalArgumentException("bad string format"); //$NON-NLS-1$
            }
            // Parsing the value for "roundingMode"...
            this.roundingMode = RoundingMode.valueOf(java.lang.StringJ.valueOf(charVal, i,charVal.Length - i).ToString());
        }
Пример #4
0
 /**
  * Constructs a new {@code MathContext} with the specified precision and
  * with the specified rounding mode. 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.
  *
  * @param precision
  *            the precision for the new {@code MathContext}.
  * @param roundingMode
  *            the rounding mode for the new {@code MathContext}.
  * @throws IllegalArgumentException
  *             if {@code precision &lt; 0}.
  * @throws NullPointerException
  *             if {@code roundingMode} is {@code null}.
  */
 public MathContext(int precision, RoundingMode roundingMode)
 {
     if (precision < 0) {
         // math.0C=Digits < 0
         throw new java.lang.IllegalArgumentException("Digits < 0"); //$NON-NLS-1$
     }
     if (roundingMode == null) {
         // math.0D=null RoundingMode
         throw new java.lang.NullPointerException("null RoundingMode"); //$NON-NLS-1$
     }
     this.precision = precision;
     this.roundingMode = roundingMode;
 }