Search in sources :

Example 21 with DecimalFormat

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

the class BigNumberFormatTest method TestPatterns.

/**
 */
@Test
public void TestPatterns() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    DecimalFormat fmt = new DecimalFormat("#", US);
    expectPad(fmt, "*^#", DecimalFormat.PAD_BEFORE_PREFIX, 1, '^');
    expectPad(fmt, "$*^#", DecimalFormat.PAD_AFTER_PREFIX, 2, '^');
    expectPad(fmt, "#*^", DecimalFormat.PAD_BEFORE_SUFFIX, 1, '^');
    expectPad(fmt, "#$*^", DecimalFormat.PAD_AFTER_SUFFIX, 2, '^');
    expectPad(fmt, "$*^$#", ILLEGAL);
    expectPad(fmt, "#$*^$", ILLEGAL);
    expectPad(fmt, "'pre'#,##0*x'post'", DecimalFormat.PAD_BEFORE_SUFFIX, 12, 'x');
    expectPad(fmt, "''#0*x", DecimalFormat.PAD_BEFORE_SUFFIX, 3, 'x');
    expectPad(fmt, "'I''ll'*a###.##", DecimalFormat.PAD_AFTER_PREFIX, 10, 'a');
    fmt.applyPattern("AA#,##0.00ZZ");
    fmt.setPadCharacter('^');
    fmt.setFormatWidth(10);
    fmt.setPadPosition(DecimalFormat.PAD_BEFORE_PREFIX);
    expectPat(fmt, "*^AA#,##0.00ZZ");
    fmt.setPadPosition(DecimalFormat.PAD_BEFORE_SUFFIX);
    expectPat(fmt, "AA#,##0.00*^ZZ");
    fmt.setPadPosition(DecimalFormat.PAD_AFTER_SUFFIX);
    expectPat(fmt, "AA#,##0.00ZZ*^");
    // 12  3456789012
    String exp = "AA*^#,##0.00ZZ";
    fmt.setFormatWidth(12);
    fmt.setPadPosition(DecimalFormat.PAD_AFTER_PREFIX);
    expectPat(fmt, exp);
    fmt.setFormatWidth(13);
    // 12  34567890123
    expectPat(fmt, "AA*^##,##0.00ZZ");
    fmt.setFormatWidth(14);
    // 12  345678901234
    expectPat(fmt, "AA*^###,##0.00ZZ");
    fmt.setFormatWidth(15);
    // 12  3456789012345
    // This is the interesting case
    expectPat(fmt, "AA*^####,##0.00ZZ");
    fmt.setFormatWidth(16);
    // 12  34567890123456
    expectPat(fmt, "AA*^#,###,##0.00ZZ");
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 22 with DecimalFormat

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

the class NumberFormatTest method TestCurrencyPatterns.

@Test
public void TestCurrencyPatterns() {
    int i;
    Locale[] locs = NumberFormat.getAvailableLocales();
    for (i = 0; i < locs.length; ++i) {
        NumberFormat nf = NumberFormat.getCurrencyInstance(locs[i]);
        // Make sure currency formats do not have a variable number
        // of fraction digits
        int min = nf.getMinimumFractionDigits();
        int max = nf.getMaximumFractionDigits();
        if (min != max) {
            String a = nf.format(1.0);
            String b = nf.format(1.125);
            errln("FAIL: " + locs[i] + " min fraction digits != max fraction digits; " + "x 1.0 => " + a + "; x 1.125 => " + b);
        }
        // Make sure EURO currency formats have exactly 2 fraction digits
        if (nf instanceof DecimalFormat) {
            Currency curr = ((DecimalFormat) nf).getCurrency();
            if (curr != null && "EUR".equals(curr.getCurrencyCode())) {
                if (min != 2 || max != 2) {
                    String a = nf.format(1.0);
                    errln("FAIL: " + locs[i] + " is a EURO format but it does not have 2 fraction digits; " + "x 1.0 => " + a);
                }
            }
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Currency(android.icu.util.Currency) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 23 with DecimalFormat

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

the class NumberFormatTest method TestParseMaxDigits.

@Test
public void TestParseMaxDigits() {
    DecimalFormat fmt = new DecimalFormat();
    String number = "100000000000";
    int newParseMax = number.length() - 1;
    fmt.setParseMaxDigits(-1);
    /* Default value is 1000 */
    if (fmt.getParseMaxDigits() != 1000) {
        errln("Fail valid value checking in setParseMaxDigits.");
    }
    try {
        if (fmt.parse(number).doubleValue() == Float.POSITIVE_INFINITY) {
            errln("Got Infinity but should NOT when parsing number: " + number);
        }
        fmt.setParseMaxDigits(newParseMax);
        if (fmt.parse(number).doubleValue() != Float.POSITIVE_INFINITY) {
            errln("Did not get Infinity but should when parsing number: " + number);
        }
    } catch (ParseException ex) {
    }
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 24 with DecimalFormat

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

the class NumberFormatTest method TestExponentParse.

@Test
public void TestExponentParse() {
    ParsePosition parsePos = new ParsePosition(0);
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
    DecimalFormat fmt = new DecimalFormat("#####", symbols);
    Number result = fmt.parse("5.06e-27", parsePos);
    if (result.doubleValue() != 5.06E-27 || parsePos.getIndex() != 8) {
        errln("ERROR: ERROR: parse failed - expected 5.06E-27, 8; got " + result.doubleValue() + ", " + parsePos.getIndex());
    }
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 25 with DecimalFormat

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

the class NumberFormatTest method TestExponential.

// Test exponential pattern
@Test
public void TestExponential() {
    DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
    final String[] pat = { "0.####E0", "00.000E00", "##0.######E000", "0.###E0;[0.###E0]" };
    int pat_length = pat.length;
    double[] val = { 0.01234, 123456789, 1.23e300, -3.141592653e-271 };
    int val_length = val.length;
    final String[] valFormat = { // 0.####E0
    "1.234E-2", "1.2346E8", "1.23E300", "-3.1416E-271", // 00.000E00
    "12.340E-03", "12.346E07", "12.300E299", "-31.416E-272", // ##0.######E000
    "12.34E-003", "123.4568E006", "1.23E300", "-314.1593E-273", // 0.###E0;[0.###E0]
    "1.234E-2", "1.235E8", "1.23E300", "[3.142E-271]" };
    /*double valParse[] =
            {
                0.01234, 123460000, 1.23E300, -3.1416E-271,
                0.01234, 123460000, 1.23E300, -3.1416E-271,
                0.01234, 123456800, 1.23E300, -3.141593E-271,
                0.01234, 123500000, 1.23E300, -3.142E-271,
            };*/
    // The variable is never used
    int[] lval = { 0, -1, 1, 123456789 };
    int lval_length = lval.length;
    final String[] lvalFormat = { // 0.####E0
    "0E0", "-1E0", "1E0", "1.2346E8", // 00.000E00
    "00.000E00", "-10.000E-01", "10.000E-01", "12.346E07", // ##0.######E000
    "0E000", "-1E000", "1E000", "123.4568E006", // 0.###E0;[0.###E0]
    "0E0", "[1E0]", "1E0", "1.235E8" };
    int[] lvalParse = { 0, -1, 1, 123460000, 0, -1, 1, 123460000, 0, -1, 1, 123456800, 0, -1, 1, 123500000 };
    int ival = 0, ilval = 0;
    for (int p = 0; p < pat_length; ++p) {
        DecimalFormat fmt = new DecimalFormat(pat[p], sym);
        logln("Pattern \"" + pat[p] + "\" -toPattern-> \"" + fmt.toPattern() + "\"");
        int v;
        for (v = 0; v < val_length; ++v) {
            String s;
            s = ((NumberFormat) fmt).format(val[v]);
            logln(" " + val[v] + " -format-> " + s);
            if (!s.equals(valFormat[v + ival]))
                errln("FAIL: Expected " + valFormat[v + ival]);
            ParsePosition pos = new ParsePosition(0);
            double a = fmt.parse(s, pos).doubleValue();
            if (pos.getIndex() == s.length()) {
                logln("  -parse-> " + Double.toString(a));
            // Use epsilon comparison as necessary
            } else
                errln("FAIL: Partial parse (" + pos.getIndex() + " chars) -> " + a);
        }
        for (v = 0; v < lval_length; ++v) {
            String s;
            s = ((NumberFormat) fmt).format(lval[v]);
            logln(" " + lval[v] + "L -format-> " + s);
            if (!s.equals(lvalFormat[v + ilval]))
                errln("ERROR: Expected " + lvalFormat[v + ilval] + " Got: " + s);
            ParsePosition pos = new ParsePosition(0);
            long a = 0;
            Number A = fmt.parse(s, pos);
            if (A != null) {
                a = A.longValue();
                if (pos.getIndex() == s.length()) {
                    logln("  -parse-> " + a);
                    if (a != lvalParse[v + ilval])
                        errln("FAIL: Expected " + lvalParse[v + ilval]);
                } else
                    errln("FAIL: Partial parse (" + pos.getIndex() + " chars) -> " + Long.toString(a));
            } else {
                errln("Fail to parse the string: " + s);
            }
        }
        ival += val_length;
        ilval += lval_length;
    }
}
Also used : 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