Search in sources :

Example 91 with DecimalFormat

use of android.icu.text.DecimalFormat 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 92 with DecimalFormat

use of android.icu.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method TestScientific.

/**
 * Upgrade to alphaWorks
 */
@Test
public void TestScientific() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    // Test pattern round-trip
    final String[] PAT = { "#E0", "0.####E0", "00.000E00", "##0.####E000", "0.###E0;[0.###E0]" };
    int PAT_length = PAT.length;
    int[] DIGITS = { // "#E0"
    0, // "#E0"
    1, // "#E0"
    0, // "#E0"
    0, // "0.####E0"
    1, // "0.####E0"
    1, // "0.####E0"
    0, // "0.####E0"
    4, // "00.000E00"
    2, // "00.000E00"
    2, // "00.000E00"
    3, // "00.000E00"
    3, // "##0.####E000"
    1, // "##0.####E000"
    3, // "##0.####E000"
    0, // "##0.####E000"
    4, // "0.###E0;[0.###E0]"
    1, // "0.###E0;[0.###E0]"
    1, // "0.###E0;[0.###E0]"
    0, // "0.###E0;[0.###E0]"
    3 };
    for (int i = 0; i < PAT_length; ++i) {
        String pat = PAT[i];
        DecimalFormat df = new DecimalFormat(pat, US);
        String pat2 = df.toPattern();
        if (pat.equals(pat2)) {
            logln("Ok   Pattern rt \"" + pat + "\" . \"" + pat2 + "\"");
        } else {
            errln("FAIL Pattern rt \"" + pat + "\" . \"" + pat2 + "\"");
        }
        // Make sure digit counts match what we expect
        if (df.getMinimumIntegerDigits() != DIGITS[4 * i] || df.getMaximumIntegerDigits() != DIGITS[4 * i + 1] || df.getMinimumFractionDigits() != DIGITS[4 * i + 2] || df.getMaximumFractionDigits() != DIGITS[4 * i + 3]) {
            errln("FAIL \"" + pat + "\" min/max int; min/max frac = " + df.getMinimumIntegerDigits() + "/" + df.getMaximumIntegerDigits() + ";" + df.getMinimumFractionDigits() + "/" + df.getMaximumFractionDigits() + ", expect " + DIGITS[4 * i] + "/" + DIGITS[4 * i + 1] + ";" + DIGITS[4 * i + 2] + "/" + DIGITS[4 * i + 3]);
        }
    }
    expect2(new DecimalFormat("#E0", US), 12345.0, "1.2345E4");
    expect(new DecimalFormat("0E0", US), 12345.0, "1E4");
    // pattern of NumberFormat.getScientificInstance(Locale.US) = "0.######E0" not "#E0"
    // so result = 1.234568E4 not 1.2345678901E4
    // when the pattern problem is finalized, delete comment mark'//'
    // of the following code
    expect2(NumberFormat.getScientificInstance(Locale.US), 12345.678901, "1.2345678901E4");
    logln("Testing NumberFormat.getScientificInstance(ULocale) ...");
    expect2(NumberFormat.getScientificInstance(ULocale.US), 12345.678901, "1.2345678901E4");
    expect(new DecimalFormat("##0.###E0", US), 12345.0, "12.34E3");
    expect(new DecimalFormat("##0.###E0", US), 12345.00001, "12.35E3");
    expect2(new DecimalFormat("##0.####E0", US), 12345, "12.345E3");
    // pattern of NumberFormat.getScientificInstance(Locale.US) = "0.######E0" not "#E0"
    // so result = 1.234568E4 not 1.2345678901E4
    expect2(NumberFormat.getScientificInstance(Locale.FRANCE), 12345.678901, "1,2345678901E4");
    logln("Testing NumberFormat.getScientificInstance(ULocale) ...");
    expect2(NumberFormat.getScientificInstance(ULocale.FRANCE), 12345.678901, "1,2345678901E4");
    expect(new DecimalFormat("##0.####E0", US), 789.12345e-9, "789.12E-9");
    expect2(new DecimalFormat("##0.####E0", US), 780.e-9, "780E-9");
    expect(new DecimalFormat(".###E0", US), 45678.0, ".457E5");
    expect2(new DecimalFormat(".###E0", US), 0, ".0E0");
    /*
        expect(new DecimalFormat[] { new DecimalFormat("#E0", US),
                                     new DecimalFormat("##E0", US),
                                     new DecimalFormat("####E0", US),
                                     new DecimalFormat("0E0", US),
                                     new DecimalFormat("00E0", US),
                                     new DecimalFormat("000E0", US),
                                   },
               new Long(45678000),
               new String[] { "4.5678E7",
                              "45.678E6",
                              "4567.8E4",
                              "5E7",
                              "46E6",
                              "457E5",
                            }
               );
        !
        ! Unroll this test into individual tests below...
        !
         */
    expect2(new DecimalFormat("#E0", US), 45678000, "4.5678E7");
    expect2(new DecimalFormat("##E0", US), 45678000, "45.678E6");
    expect2(new DecimalFormat("####E0", US), 45678000, "4567.8E4");
    expect(new DecimalFormat("0E0", US), 45678000, "5E7");
    expect(new DecimalFormat("00E0", US), 45678000, "46E6");
    expect(new DecimalFormat("000E0", US), 45678000, "457E5");
    /*
        expect(new DecimalFormat("###E0", US, status),
               new Object[] { new Double(0.0000123), "12.3E-6",
                              new Double(0.000123), "123E-6",
                              new Double(0.00123), "1.23E-3",
                              new Double(0.0123), "12.3E-3",
                              new Double(0.123), "123E-3",
                              new Double(1.23), "1.23E0",
                              new Double(12.3), "12.3E0",
                              new Double(123), "123E0",
                              new Double(1230), "1.23E3",
                             });
        !
        ! Unroll this test into individual tests below...
        !
         */
    expect2(new DecimalFormat("###E0", US), 0.0000123, "12.3E-6");
    expect2(new DecimalFormat("###E0", US), 0.000123, "123E-6");
    expect2(new DecimalFormat("###E0", US), 0.00123, "1.23E-3");
    expect2(new DecimalFormat("###E0", US), 0.0123, "12.3E-3");
    expect2(new DecimalFormat("###E0", US), 0.123, "123E-3");
    expect2(new DecimalFormat("###E0", US), 1.23, "1.23E0");
    expect2(new DecimalFormat("###E0", US), 12.3, "12.3E0");
    expect2(new DecimalFormat("###E0", US), 123.0, "123E0");
    expect2(new DecimalFormat("###E0", US), 1230.0, "1.23E3");
    /*
        expect(new DecimalFormat("0.#E+00", US, status),
               new Object[] { new Double(0.00012), "1.2E-04",
                              new Long(12000),     "1.2E+04",
                             });
        !
        ! Unroll this test into individual tests below...
        !
         */
    expect2(new DecimalFormat("0.#E+00", US), 0.00012, "1.2E-04");
    expect2(new DecimalFormat("0.#E+00", US), 12000, "1.2E+04");
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 93 with DecimalFormat

use of android.icu.text.DecimalFormat 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 94 with DecimalFormat

use of android.icu.text.DecimalFormat 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 95 with DecimalFormat

use of android.icu.text.DecimalFormat in project j2objc by google.

the class NumberFormatTest method TestNPEIssue11735.

// Testing for Issue 11735.
@Test
public void TestNPEIssue11735() {
    DecimalFormat fmt = new DecimalFormat("0", new DecimalFormatSymbols(new ULocale("en")));
    ParsePosition ppos = new ParsePosition(0);
    assertEquals("Currency symbol missing in parse. Expect null result.", fmt.parseCurrency("53.45", ppos), null);
}
Also used : ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Aggregations

DecimalFormat (android.icu.text.DecimalFormat)150 Test (org.junit.Test)138 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)70 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)57 NumberFormat (android.icu.text.NumberFormat)30 ULocale (android.icu.util.ULocale)28 FieldPosition (java.text.FieldPosition)25 ParseException (java.text.ParseException)23 Locale (java.util.Locale)18 ParsePosition (java.text.ParsePosition)15 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)12 BigDecimal (android.icu.math.BigDecimal)10 IOException (java.io.IOException)8 InvalidObjectException (java.io.InvalidObjectException)5 BigInteger (java.math.BigInteger)5 ScientificNumberFormatter (android.icu.text.ScientificNumberFormatter)4 SimpleDateFormat (android.icu.text.SimpleDateFormat)4 Format (java.text.Format)4 DateFormat (android.icu.text.DateFormat)3 MessageFormat (android.icu.text.MessageFormat)3