Search in sources :

Example 16 with BigDecimal

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

the class NumberFormatTest method TestMissingFieldPositionsNegativeBigDec.

@Test
public void TestMissingFieldPositionsNegativeBigDec() {
    // Check complex positive;negative pattern.
    DecimalFormatSymbols us_symbols = new DecimalFormatSymbols(ULocale.US);
    DecimalFormat fmtPosNegSign = new DecimalFormat("+0.####E+00;-0.#######E+0", us_symbols);
    Number negativeExp = new BigDecimal("-0.000000987654321083");
    String negExpFormatted = fmtPosNegSign.format(negativeExp);
    checkFormatWithField("sign", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.SIGN, 0, 1);
    checkFormatWithField("integer", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.INTEGER, 1, 2);
    checkFormatWithField("decimal separator", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.DECIMAL_SEPARATOR, 2, 3);
    checkFormatWithField("fraction", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.FRACTION, 3, 7);
    checkFormatWithField("exponent symbol", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.EXPONENT_SYMBOL, 7, 8);
    checkFormatWithField("exponent sign", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.EXPONENT_SIGN, 8, 9);
    checkFormatWithField("exponent", fmtPosNegSign, negativeExp, negExpFormatted, NumberFormat.Field.EXPONENT, 9, 11);
}
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 17 with BigDecimal

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

the class NumberFormatTest method TestParseReturnType.

@Test
public void TestParseReturnType() {
    String[] defaultNonBigDecimals = { // Long
    "123", // Long
    "123.0", // Long
    "0.0", // BigInteger
    "12345678901234567890" };
    String[] doubles = { "-0.0", "NaN", // Infinity
    "\u221E" };
    DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
    DecimalFormat nf = new DecimalFormat("#.#", sym);
    if (nf.isParseBigDecimal()) {
        errln("FAIL: isParseDecimal() must return false by default");
    }
    // isParseBigDecimal() is false
    for (int i = 0; i < defaultNonBigDecimals.length; i++) {
        try {
            Number n = nf.parse(defaultNonBigDecimals[i]);
            if (n instanceof BigDecimal) {
                errln("FAIL: parse returns BigDecimal instance");
            }
        } catch (ParseException e) {
            errln("parse of '" + defaultNonBigDecimals[i] + "' threw exception: " + e);
        }
    }
    // parse results for doubls must be always Double
    for (int i = 0; i < doubles.length; i++) {
        try {
            Number n = nf.parse(doubles[i]);
            if (!(n instanceof Double)) {
                errln("FAIL: parse does not return Double instance");
            }
        } catch (ParseException e) {
            errln("parse of '" + doubles[i] + "' threw exception: " + e);
        }
    }
    // force this DecimalFormat to return BigDecimal
    nf.setParseBigDecimal(true);
    if (!nf.isParseBigDecimal()) {
        errln("FAIL: isParseBigDecimal() must return true");
    }
    // isParseBigDecimal() is true
    for (int i = 0; i < defaultNonBigDecimals.length; i++) {
        try {
            Number n = nf.parse(defaultNonBigDecimals[i]);
            if (!(n instanceof BigDecimal)) {
                errln("FAIL: parse does not return BigDecimal instance");
            }
        } catch (ParseException e) {
            errln("parse of '" + defaultNonBigDecimals[i] + "' threw exception: " + e);
        }
    }
    // parse results for doubls must be always Double
    for (int i = 0; i < doubles.length; i++) {
        try {
            Number n = nf.parse(doubles[i]);
            if (!(n instanceof Double)) {
                errln("FAIL: parse does not return Double instance");
            }
        } catch (ParseException e) {
            errln("parse of '" + doubles[i] + "' threw exception: " + e);
        }
    }
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ParseException(java.text.ParseException) BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 18 with BigDecimal

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

the class NumberFormatTest method TestRoundingBehavior.

@Test
public void TestRoundingBehavior() {
    final Object[][] TEST_CASES = { { // ULocale - null for default locale
    ULocale.US, // Pattern
    "#.##", // Rounding Mode or null (implicit)
    Integer.valueOf(BigDecimal.ROUND_DOWN), // Rounding increment, Double or BigDecimal, or null (implicit)
    Double.valueOf(0.0d), // Input value, Long, Double, BigInteger or BigDecimal
    Double.valueOf(123.4567d), // Expected result, null for exception
    "123.45" }, { ULocale.US, "#.##", null, Double.valueOf(0.1d), Double.valueOf(123.4567d), "123.5" }, { ULocale.US, "#.##", Integer.valueOf(BigDecimal.ROUND_DOWN), Double.valueOf(0.1d), Double.valueOf(123.4567d), "123.4" }, { ULocale.US, "#.##", Integer.valueOf(BigDecimal.ROUND_UNNECESSARY), null, Double.valueOf(123.4567d), null }, { ULocale.US, "#.##", Integer.valueOf(BigDecimal.ROUND_DOWN), null, Long.valueOf(1234), "1234" } };
    int testNum = 1;
    for (Object[] testCase : TEST_CASES) {
        // 0: locale
        // 1: pattern
        ULocale locale = testCase[0] == null ? ULocale.getDefault() : (ULocale) testCase[0];
        String pattern = (String) testCase[1];
        DecimalFormat fmt = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(locale));
        // 2: rounding mode
        Integer roundingMode = null;
        if (testCase[2] != null) {
            roundingMode = (Integer) testCase[2];
            fmt.setRoundingMode(roundingMode);
        }
        // 3: rounding increment
        if (testCase[3] != null) {
            if (testCase[3] instanceof Double) {
                fmt.setRoundingIncrement((Double) testCase[3]);
            } else if (testCase[3] instanceof BigDecimal) {
                fmt.setRoundingIncrement((BigDecimal) testCase[3]);
            } else if (testCase[3] instanceof java.math.BigDecimal) {
                fmt.setRoundingIncrement((java.math.BigDecimal) testCase[3]);
            }
        }
        // 4: input number
        String s = null;
        boolean bException = false;
        try {
            s = fmt.format(testCase[4]);
        } catch (ArithmeticException e) {
            bException = true;
        }
        if (bException) {
            if (testCase[5] != null) {
                errln("Test case #" + testNum + ": ArithmeticException was thrown.");
            }
        } else {
            if (testCase[5] == null) {
                errln("Test case #" + testNum + ": ArithmeticException must be thrown, but got formatted result: " + s);
            } else {
                assertEquals("Test case #" + testNum, testCase[5], s);
            }
        }
        testNum++;
    }
}
Also used : ULocale(android.icu.util.ULocale) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) BigDecimal(android.icu.math.BigDecimal) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 19 with BigDecimal

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

the class NumberFormatTest method TestRoundingPattern.

@Test
public void TestRoundingPattern() {
    class TestRoundingPatternItem {

        String pattern;

        double roundingIncrement;

        double testCase;

        String expected;

        TestRoundingPatternItem(String pattern, double roundingIncrement, double testCase, String expected) {
            this.pattern = pattern;
            this.roundingIncrement = roundingIncrement;
            this.testCase = testCase;
            this.expected = expected;
        }
    }
    ;
    TestRoundingPatternItem[] tests = { new TestRoundingPatternItem("##0.65", 0.65, 1.234, "1.30"), new TestRoundingPatternItem("#50", 50.0, 1230, "1250") };
    DecimalFormat df = (DecimalFormat) android.icu.text.NumberFormat.getInstance(ULocale.ENGLISH);
    String result;
    BigDecimal bd;
    for (int i = 0; i < tests.length; i++) {
        df.applyPattern(tests[i].pattern);
        result = df.format(tests[i].testCase);
        if (!tests[i].expected.equals(result)) {
            errln("String Pattern Rounding Test Failed: Pattern: \"" + tests[i].pattern + "\" Number: " + tests[i].testCase + " - Got: " + result + " Expected: " + tests[i].expected);
        }
        bd = new BigDecimal(tests[i].roundingIncrement);
        df.setRoundingIncrement(bd);
        result = df.format(tests[i].testCase);
        if (!tests[i].expected.equals(result)) {
            errln("BigDecimal Rounding Test Failed: Pattern: \"" + tests[i].pattern + "\" Number: " + tests[i].testCase + " - Got: " + result + " Expected: " + tests[i].expected);
        }
    }
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) BigDecimal(android.icu.math.BigDecimal) Test(org.junit.Test)

Example 20 with BigDecimal

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

the class NumberFormatTest method TestFormatAbstractImplCoverage.

/*
     * Coverage tests for the implementation of abstract format methods not being called otherwise
     */
public void TestFormatAbstractImplCoverage() {
    NumberFormat df = DecimalFormat.getInstance(Locale.ENGLISH);
    NumberFormat cdf = CompactDecimalFormat.getInstance(Locale.ENGLISH, CompactDecimalFormat.CompactStyle.SHORT);
    NumberFormat rbf = new RuleBasedNumberFormat(ULocale.ENGLISH, RuleBasedNumberFormat.SPELLOUT);
    /*
         *  Test  NumberFormat.format(BigDecimal,StringBuffer,FieldPosition)
         */
    StringBuffer sb = new StringBuffer();
    String result = df.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
    if (!"2,000.43".equals(result)) {
        errln("DecimalFormat failed. Expected: 2,000.43 - Actual: " + result);
    }
    sb.delete(0, sb.length());
    result = cdf.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
    if (!"2K".equals(result)) {
        errln("DecimalFormat failed. Expected: 2K - Actual: " + result);
    }
    sb.delete(0, sb.length());
    result = rbf.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
    if (!"two thousand point four three".equals(result)) {
        errln("DecimalFormat failed. Expected: 'two thousand point four three' - Actual: '" + result + "'");
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) FieldPosition(java.text.FieldPosition) BigDecimal(android.icu.math.BigDecimal) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat)

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