Search in sources :

Example 21 with BigDecimal

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

the class NumberFormatTest method checkRounding.

void checkRounding(DecimalFormat nf, BigDecimal base, int iterations, BigDecimal increment) {
    nf.setRoundingIncrement(increment.toBigDecimal());
    // used to make sure that rounding is monotonic
    BigDecimal lastParsed = new BigDecimal(Integer.MIN_VALUE);
    for (int i = -iterations; i <= iterations; ++i) {
        BigDecimal iValue = base.add(increment.multiply(new BigDecimal(i)).movePointLeft(1));
        BigDecimal smallIncrement = new BigDecimal("0.00000001");
        if (iValue.signum() != 0) {
            // scale unless zero
            smallIncrement = smallIncrement.multiply(iValue);
        }
        // we not only test the value, but some values in a small range around it.
        lastParsed = checkRound(nf, iValue.subtract(smallIncrement), lastParsed);
        lastParsed = checkRound(nf, iValue, lastParsed);
        lastParsed = checkRound(nf, iValue.add(smallIncrement), lastParsed);
    }
}
Also used : BigDecimal(android.icu.math.BigDecimal)

Example 22 with BigDecimal

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

the class NumberFormatTest method TestNPEIssue11914.

// Testing for Issue 11914, missing FieldPositions for some field types.
@Test
public void TestNPEIssue11914() {
    // First test: Double value with grouping separators.
    List<FieldContainer> v1 = new ArrayList<FieldContainer>(7);
    v1.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));
    v1.add(new FieldContainer(3, 4, NumberFormat.Field.GROUPING_SEPARATOR));
    v1.add(new FieldContainer(4, 7, NumberFormat.Field.INTEGER));
    v1.add(new FieldContainer(7, 8, NumberFormat.Field.GROUPING_SEPARATOR));
    v1.add(new FieldContainer(8, 11, NumberFormat.Field.INTEGER));
    v1.add(new FieldContainer(11, 12, NumberFormat.Field.DECIMAL_SEPARATOR));
    v1.add(new FieldContainer(12, 15, NumberFormat.Field.FRACTION));
    Number number = new Double(123456789.9753);
    ULocale usLoc = new ULocale("en-US");
    DecimalFormatSymbols US = new DecimalFormatSymbols(usLoc);
    NumberFormat outFmt = NumberFormat.getNumberInstance(usLoc);
    String numFmtted = outFmt.format(number);
    AttributedCharacterIterator iterator = outFmt.formatToCharacterIterator(number);
    CompareAttributedCharacterFormatOutput(iterator, v1, numFmtted);
    // Second test: Double with scientific notation formatting.
    List<FieldContainer> v2 = new ArrayList<FieldContainer>(7);
    v2.add(new FieldContainer(0, 1, NumberFormat.Field.INTEGER));
    v2.add(new FieldContainer(1, 2, NumberFormat.Field.DECIMAL_SEPARATOR));
    v2.add(new FieldContainer(2, 5, NumberFormat.Field.FRACTION));
    v2.add(new FieldContainer(5, 6, NumberFormat.Field.EXPONENT_SYMBOL));
    v2.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SIGN));
    v2.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT));
    DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US);
    numFmtted = fmt2.format(number);
    iterator = fmt2.formatToCharacterIterator(number);
    CompareAttributedCharacterFormatOutput(iterator, v2, numFmtted);
    // Third test. BigInteger with grouping separators.
    List<FieldContainer> v3 = new ArrayList<FieldContainer>(7);
    v3.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN));
    v3.add(new FieldContainer(1, 2, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(2, 3, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(3, 6, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(6, 7, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(7, 10, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(10, 11, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(11, 14, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(14, 15, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(15, 18, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(18, 19, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(19, 22, NumberFormat.Field.INTEGER));
    v3.add(new FieldContainer(22, 23, NumberFormat.Field.GROUPING_SEPARATOR));
    v3.add(new FieldContainer(23, 26, NumberFormat.Field.INTEGER));
    BigInteger bigNumberInt = new BigInteger("-1234567890246813579");
    String fmtNumberBigInt = outFmt.format(bigNumberInt);
    iterator = outFmt.formatToCharacterIterator(bigNumberInt);
    CompareAttributedCharacterFormatOutput(iterator, v3, fmtNumberBigInt);
    // Fourth test: BigDecimal with exponential formatting.
    List<FieldContainer> v4 = new ArrayList<FieldContainer>(7);
    v4.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN));
    v4.add(new FieldContainer(1, 2, NumberFormat.Field.INTEGER));
    v4.add(new FieldContainer(2, 3, NumberFormat.Field.DECIMAL_SEPARATOR));
    v4.add(new FieldContainer(3, 6, NumberFormat.Field.FRACTION));
    v4.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SYMBOL));
    v4.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT_SIGN));
    v4.add(new FieldContainer(8, 9, NumberFormat.Field.EXPONENT));
    java.math.BigDecimal numberBigD = new java.math.BigDecimal(-123456789);
    String fmtNumberBigDExp = fmt2.format(numberBigD);
    iterator = fmt2.formatToCharacterIterator(numberBigD);
    CompareAttributedCharacterFormatOutput(iterator, v4, fmtNumberBigDExp);
}
Also used : ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ArrayList(java.util.ArrayList) BigDecimal(android.icu.math.BigDecimal) AttributedCharacterIterator(java.text.AttributedCharacterIterator) BigInteger(java.math.BigInteger) FieldContainer(android.icu.dev.test.format.IntlTestDecimalFormatAPIC.FieldContainer) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 23 with BigDecimal

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

the class NumberFormatTest method TestMissingFieldPositionsNegativeBigInt.

@Test
public void TestMissingFieldPositionsNegativeBigInt() {
    DecimalFormatSymbols us_symbols = new DecimalFormatSymbols(ULocale.US);
    DecimalFormat formatter = new DecimalFormat("0.#####E+0", us_symbols);
    Number number = new BigDecimal("-123456789987654321");
    String bigDecFmtted = formatter.format(number);
    checkFormatWithField("sign", formatter, number, bigDecFmtted, NumberFormat.Field.SIGN, 0, 1);
    checkFormatWithField("integer", formatter, number, bigDecFmtted, NumberFormat.Field.INTEGER, 1, 2);
    checkFormatWithField("decimal separator", formatter, number, bigDecFmtted, NumberFormat.Field.DECIMAL_SEPARATOR, 2, 3);
    checkFormatWithField("exponent symbol", formatter, number, bigDecFmtted, NumberFormat.Field.EXPONENT_SYMBOL, 8, 9);
    checkFormatWithField("exponent sign", formatter, number, bigDecFmtted, NumberFormat.Field.EXPONENT_SIGN, 9, 10);
    checkFormatWithField("exponent", formatter, number, bigDecFmtted, NumberFormat.Field.EXPONENT, 10, 12);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 24 with BigDecimal

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

the class NumberFormatTest method TestCurrencyFormatForMixParsing.

@Test
public void TestCurrencyFormatForMixParsing() {
    MeasureFormat curFmt = MeasureFormat.getCurrencyFormat(new ULocale("en_US"));
    String[] formats = { // string to be parsed
    "$1,234.56", "USD1,234.56", "US dollars1,234.56", "1,234.56 US dollars" };
    try {
        for (int i = 0; i < formats.length; ++i) {
            String stringToBeParsed = formats[i];
            CurrencyAmount parsedVal = (CurrencyAmount) curFmt.parseObject(stringToBeParsed);
            Number val = parsedVal.getNumber();
            if (!val.equals(new BigDecimal("1234.56"))) {
                errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the number. val=" + val);
            }
            if (!parsedVal.getCurrency().equals(Currency.getInstance("USD"))) {
                errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the currency");
            }
        }
    } catch (ParseException e) {
        errln("parse FAILED: " + e.toString());
    }
}
Also used : ULocale(android.icu.util.ULocale) ParseException(java.text.ParseException) MeasureFormat(android.icu.text.MeasureFormat) CurrencyAmount(android.icu.util.CurrencyAmount) BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 25 with BigDecimal

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

the class NumberFormatTest method TestRounding.

@Test
public void TestRounding() {
    DecimalFormat nf = (DecimalFormat) android.icu.text.NumberFormat.getInstance(ULocale.ENGLISH);
    if (false) {
        // for debugging specific value
        nf.setRoundingMode(BigDecimal.ROUND_HALF_UP);
        checkRounding(nf, new BigDecimal("300.0300000000"), 0, new BigDecimal("0.020000000"));
    }
    // full tests
    int[] roundingIncrements = { 1, 2, 5, 20, 50, 100 };
    int[] testValues = { 0, 300 };
    for (int j = 0; j < testValues.length; ++j) {
        for (int mode = BigDecimal.ROUND_UP; mode < BigDecimal.ROUND_HALF_EVEN; ++mode) {
            nf.setRoundingMode(mode);
            for (int increment = 0; increment < roundingIncrements.length; ++increment) {
                BigDecimal base = new BigDecimal(testValues[j]);
                BigDecimal rInc = new BigDecimal(roundingIncrements[increment]);
                checkRounding(nf, base, 20, rInc);
                rInc = new BigDecimal("1.000000000").divide(rInc);
                checkRounding(nf, base, 20, rInc);
            }
        }
    }
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

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