Search in sources :

Example 26 with BigDecimal

use of android.icu.math.BigDecimal in project j2objc by google.

the class TimeScaleAPITest method TestToBigDecimalTrunc.

@Test
public void TestToBigDecimalTrunc() {
    BigDecimal bigZero = new BigDecimal(0);
    try {
        UniversalTimeScale.toBigDecimalTrunc(bigZero, -1);
        errln("toBigDecimalTrunc(bigZero, -1) did not throw IllegalArgumentException.");
    } catch (IllegalArgumentException iae) {
        logln("PASS: UniversalTimeScale.toBigDecimalTrunc failed as expected");
    }
    for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
        try {
            UniversalTimeScale.toBigDecimalTrunc(bigZero, scale);
        } catch (IllegalArgumentException iae) {
            errln("toBigDecimalTrunc(bigZero, " + scale + ") threw IllegalArgumentException.");
        }
    }
    try {
        UniversalTimeScale.toBigDecimalTrunc(bigZero, UniversalTimeScale.MAX_SCALE);
        errln("toBigDecimalTrunc(bigZero, MAX_SCALE) did not throw IllegalArgumetException.");
    } catch (IllegalArgumentException iae) {
        logln("PASS: UniversalTimeScale.toBigDecimalTrunc failed as expected");
    }
}
Also used : BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 27 with BigDecimal

use of android.icu.math.BigDecimal in project j2objc by google.

the class TimeScaleAPITest method TestBigDecimalFromBigDecimal.

@Test
public void TestBigDecimalFromBigDecimal() {
    BigDecimal bigZero = new BigDecimal(0);
    try {
        UniversalTimeScale.bigDecimalFrom(bigZero, -1);
        errln("bigDecimalFrom(bigZero, -1) did not throw IllegalArgumentException.");
    } catch (IllegalArgumentException iae) {
        logln("PASS: UniversalTimeScale.bigDecimalFrom failed as expected");
    }
    for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
        try {
            UniversalTimeScale.bigDecimalFrom(bigZero, scale);
        } catch (IllegalArgumentException iae) {
            errln("bigDecimalFrom(bigZero, " + scale + ") threw IllegalArgumentException.");
        }
    }
    try {
        UniversalTimeScale.bigDecimalFrom(bigZero, UniversalTimeScale.MAX_SCALE);
        errln("from(bigZero, MAX_SCALE) did not throw IllegalArgumetException.");
    } catch (IllegalArgumentException iae) {
        logln("PASS: UniversalTimeScale.bigDecimalFrom failed as expected");
    }
}
Also used : BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 28 with BigDecimal

use of android.icu.math.BigDecimal in project j2objc by google.

the class DecimalFormat method parse.

/**
 * Parses the given text as either a Number or a CurrencyAmount.
 *
 * @param text the string to parse
 * @param parsePosition input-output position; on input, the position within text to
 * match; must have 0 <= pos.getIndex() < text.length(); on output, the position after
 * the last matched character. If the parse fails, the position in unchanged upon
 * output.
 * @param currency if non-null, a CurrencyAmount is parsed and returned; otherwise a
 * Number is parsed and returned
 * @return a Number or CurrencyAmount or null
 */
private Object parse(String text, ParsePosition parsePosition, Currency[] currency) {
    int backup;
    int i = backup = parsePosition.getIndex();
    // Skip padding characters, if around prefix
    if (formatWidth > 0 && (padPosition == PAD_BEFORE_PREFIX || padPosition == PAD_AFTER_PREFIX)) {
        i = skipPadding(text, i);
    }
    if (text.regionMatches(i, symbols.getNaN(), 0, symbols.getNaN().length())) {
        i += symbols.getNaN().length();
        // Skip padding characters, if around suffix
        if (formatWidth > 0 && (padPosition == PAD_BEFORE_SUFFIX || padPosition == PAD_AFTER_SUFFIX)) {
            i = skipPadding(text, i);
        }
        parsePosition.setIndex(i);
        return new Double(Double.NaN);
    }
    // NaN parse failed; start over
    i = backup;
    boolean[] status = new boolean[STATUS_LENGTH];
    if (currencySignCount != CURRENCY_SIGN_COUNT_ZERO) {
        if (!parseForCurrency(text, parsePosition, currency, status)) {
            return null;
        }
    } else if (currency != null) {
        return null;
    } else {
        if (!subparse(text, parsePosition, digitList, status, currency, negPrefixPattern, negSuffixPattern, posPrefixPattern, posSuffixPattern, false, Currency.SYMBOL_NAME)) {
            parsePosition.setIndex(backup);
            return null;
        }
    }
    Number n = null;
    // Handle infinity
    if (status[STATUS_INFINITE]) {
        n = new Double(status[STATUS_POSITIVE] ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY);
    } else // Handle underflow
    if (status[STATUS_UNDERFLOW]) {
        n = status[STATUS_POSITIVE] ? new Double("0.0") : new Double("-0.0");
    } else // Handle -0.0
    if (!status[STATUS_POSITIVE] && digitList.isZero()) {
        n = new Double("-0.0");
    } else {
        // Do as much of the multiplier conversion as possible without
        // losing accuracy.
        // Don't modify this.multiplier
        int mult = multiplier;
        while (mult % 10 == 0) {
            --digitList.decimalAt;
            mult /= 10;
        }
        // Handle integral values
        if (!parseBigDecimal && mult == 1 && digitList.isIntegral()) {
            // hack quick long
            if (digitList.decimalAt < 12) {
                // quick check for long
                long l = 0;
                if (digitList.count > 0) {
                    int nx = 0;
                    while (nx < digitList.count) {
                        l = l * 10 + (char) digitList.digits[nx++] - '0';
                    }
                    while (nx++ < digitList.decimalAt) {
                        l *= 10;
                    }
                    if (!status[STATUS_POSITIVE]) {
                        l = -l;
                    }
                }
                n = Long.valueOf(l);
            } else {
                BigInteger big = digitList.getBigInteger(status[STATUS_POSITIVE]);
                n = (big.bitLength() < 64) ? (Number) Long.valueOf(big.longValue()) : (Number) big;
            }
        } else // Handle non-integral values or the case where parseBigDecimal is set
        {
            BigDecimal big = digitList.getBigDecimalICU(status[STATUS_POSITIVE]);
            n = big;
            if (mult != 1) {
                n = big.divide(BigDecimal.valueOf(mult), mathContext);
            }
        }
    }
    // Assemble into CurrencyAmount if necessary
    return (currency != null) ? (Object) new CurrencyAmount(n, currency[0]) : (Object) n;
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(android.icu.math.BigDecimal) CurrencyAmount(android.icu.util.CurrencyAmount)

Example 29 with BigDecimal

use of android.icu.math.BigDecimal in project j2objc by google.

the class DecimalFormat method readObject.

/**
 * First, read the default serializable fields from the stream. Then if
 * <code>serialVersionOnStream</code> is less than 1, indicating that the stream was
 * written by JDK 1.1, initialize <code>useExponentialNotation</code> to false, since
 * it was not present in JDK 1.1. Finally, set serialVersionOnStream back to the
 * maximum allowed value so that default serialization will work properly if this
 * object is streamed out again.
 */
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
    stream.defaultReadObject();
    if (getMaximumIntegerDigits() > MAX_INTEGER_DIGITS) {
        setMaximumIntegerDigits(MAX_INTEGER_DIGITS);
    }
    if (getMaximumFractionDigits() > DOUBLE_FRACTION_DIGITS) {
        _setMaximumFractionDigits(DOUBLE_FRACTION_DIGITS);
    }
    if (serialVersionOnStream < 2) {
        exponentSignAlwaysShown = false;
        setInternalRoundingIncrement(null);
        roundingMode = BigDecimal.ROUND_HALF_EVEN;
        formatWidth = 0;
        pad = ' ';
        padPosition = PAD_BEFORE_PREFIX;
        if (serialVersionOnStream < 1) {
            // Didn't have exponential fields
            useExponentialNotation = false;
        }
    }
    if (serialVersionOnStream < 3) {
        // Versions prior to 3 do not store a currency object.  Create one to match
        // the DecimalFormatSymbols object.
        setCurrencyForSymbols();
    }
    if (serialVersionOnStream < 4) {
        currencyUsage = CurrencyUsage.STANDARD;
    }
    serialVersionOnStream = currentSerialVersion;
    digitList = new DigitList();
    if (roundingIncrement != null) {
        setInternalRoundingIncrement(new BigDecimal(roundingIncrement));
    }
    resetActualRounding();
}
Also used : BigDecimal(android.icu.math.BigDecimal)

Example 30 with BigDecimal

use of android.icu.math.BigDecimal in project j2objc by google.

the class DecimalFormat method resetActualRounding.

/*
     * Set roundingDouble, roundingDoubleReciprocal and actualRoundingIncrement
     * based on rounding mode and width of fractional digits. Whenever setting affecting
     * rounding mode, rounding increment and maximum width of fractional digits, then
     * this method must be called.
     *
     * roundingIncrementICU is the field storing the custom rounding increment value,
     * while actual rounding increment could be larger.
     */
private void resetActualRounding() {
    if (roundingIncrementICU != null) {
        BigDecimal byWidth = getMaximumFractionDigits() > 0 ? BigDecimal.ONE.movePointLeft(getMaximumFractionDigits()) : BigDecimal.ONE;
        if (roundingIncrementICU.compareTo(byWidth) >= 0) {
            actualRoundingIncrementICU = roundingIncrementICU;
        } else {
            actualRoundingIncrementICU = byWidth.equals(BigDecimal.ONE) ? null : byWidth;
        }
    } else {
        if (roundingMode == BigDecimal.ROUND_HALF_EVEN || isScientificNotation()) {
            // This rounding fix is irrelevant if mode is ROUND_HALF_EVEN as DigitList
            // does ROUND_HALF_EVEN for us.  This rounding fix won't work at all for
            // scientific notation.
            actualRoundingIncrementICU = null;
        } else {
            if (getMaximumFractionDigits() > 0) {
                actualRoundingIncrementICU = BigDecimal.ONE.movePointLeft(getMaximumFractionDigits());
            } else {
                actualRoundingIncrementICU = BigDecimal.ONE;
            }
        }
    }
    if (actualRoundingIncrementICU == null) {
        setRoundingDouble(0.0d);
        actualRoundingIncrement = null;
    } else {
        setRoundingDouble(actualRoundingIncrementICU.doubleValue());
        actualRoundingIncrement = actualRoundingIncrementICU.toBigDecimal();
    }
}
Also used : BigDecimal(android.icu.math.BigDecimal)

Aggregations

BigDecimal (android.icu.math.BigDecimal)33 Test (org.junit.Test)19 DecimalFormat (android.icu.text.DecimalFormat)10 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)9 BigInteger (java.math.BigInteger)5 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)4 ULocale (android.icu.util.ULocale)4 ParseException (java.text.ParseException)4 NumberFormat (android.icu.text.NumberFormat)3 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)3 CurrencyAmount (android.icu.util.CurrencyAmount)3 MeasureFormat (android.icu.text.MeasureFormat)2 FieldPosition (java.text.FieldPosition)2 FieldContainer (android.icu.dev.test.format.IntlTestDecimalFormatAPIC.FieldContainer)1 Currency (android.icu.util.Currency)1 AttributedCharacterIterator (java.text.AttributedCharacterIterator)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1