Search in sources :

Example 71 with NumberFormat

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

the class NumberFormatTest method TestSecondaryGrouping.

/**
 * Test the functioning of the secondary grouping value.
 */
@Test
public void TestSecondaryGrouping() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    DecimalFormat f = new DecimalFormat("#,##,###", US);
    expect(f, 123456789L, "12,34,56,789");
    expectPat(f, "#,##,###");
    f.applyPattern("#,###");
    f.setSecondaryGroupingSize(4);
    expect(f, 123456789L, "12,3456,789");
    expectPat(f, "#,####,###");
    NumberFormat g = NumberFormat.getInstance(new Locale("hi", "IN"));
    String out = "";
    long l = 1876543210L;
    out = g.format(l);
    // expect "1,87,65,43,210", but with Hindi digits
    // 01234567890123
    boolean ok = true;
    if (out.length() != 14) {
        ok = false;
    } else {
        for (int i = 0; i < out.length(); ++i) {
            boolean expectGroup = false;
            switch(i) {
                case 1:
                case 4:
                case 7:
                case 10:
                    expectGroup = true;
                    break;
            }
            // Later -- fix this to get the actual grouping
            // character from the resource bundle.
            boolean isGroup = (out.charAt(i) == 0x002C);
            if (isGroup != expectGroup) {
                ok = false;
                break;
            }
        }
    }
    if (!ok) {
        errln("FAIL  Expected " + l + " x hi_IN . \"1,87,65,43,210\" (with Hindi digits), got \"" + out + "\"");
    } else {
        logln("Ok    " + l + " x hi_IN . \"" + out + "\"");
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 72 with NumberFormat

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

the class NumberFormatTest method Test6816.

@Test
public void Test6816() {
    Currency cur1 = Currency.getInstance(new Locale("und", "PH"));
    NumberFormat nfmt = NumberFormat.getCurrencyInstance(new Locale("und", "PH"));
    DecimalFormatSymbols decsym = ((DecimalFormat) nfmt).getDecimalFormatSymbols();
    Currency cur2 = decsym.getCurrency();
    if (!cur1.getCurrencyCode().equals("PHP") || !cur2.getCurrencyCode().equals("PHP")) {
        errln("FAIL: Currencies should match PHP: cur1 = " + cur1.getCurrencyCode() + "; cur2 = " + cur2.getCurrencyCode());
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) Currency(android.icu.util.Currency) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 73 with NumberFormat

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

the class NumberFormatTest method TestPatterns.

// Test various patterns
@Test
public void TestPatterns() {
    DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
    final String[] pat = { "#.#", "#.", ".#", "#" };
    int pat_length = pat.length;
    final String[] newpat = { "#0.#", "#0.", "#.0", "#" };
    final String[] num = { "0", "0.", ".0", "0" };
    for (int i = 0; i < pat_length; ++i) {
        DecimalFormat fmt = new DecimalFormat(pat[i], sym);
        String newp = fmt.toPattern();
        if (!newp.equals(newpat[i]))
            errln("FAIL: Pattern " + pat[i] + " should transmute to " + newpat[i] + "; " + newp + " seen instead");
        String s = ((NumberFormat) fmt).format(0);
        if (!s.equals(num[i])) {
            errln("FAIL: Pattern " + pat[i] + " should format zero as " + num[i] + "; " + s + " seen instead");
            logln("Min integer digits = " + fmt.getMinimumIntegerDigits());
        }
        // BigInteger 0 - ticket#4731
        s = ((NumberFormat) fmt).format(BigInteger.ZERO);
        if (!s.equals(num[i])) {
            errln("FAIL: Pattern " + pat[i] + " should format BigInteger zero as " + num[i] + "; " + s + " seen instead");
            logln("Min integer digits = " + fmt.getMinimumIntegerDigits());
        }
    }
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 74 with NumberFormat

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

the class NumberFormatTest method TestCurrencyKeyword.

@Test
public void TestCurrencyKeyword() {
    ULocale locale = new ULocale("th_TH@currency=QQQ");
    NumberFormat format = NumberFormat.getCurrencyInstance(locale);
    String result = format.format(12.34f);
    if (!"QQQ12.34".equals(result)) {
        errln("got unexpected currency: " + result);
    }
}
Also used : ULocale(android.icu.util.ULocale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 75 with NumberFormat

use of android.icu.text.NumberFormat 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)

Aggregations

NumberFormat (android.icu.text.NumberFormat)105 Test (org.junit.Test)84 ULocale (android.icu.util.ULocale)39 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)35 DecimalFormat (android.icu.text.DecimalFormat)27 Locale (java.util.Locale)24 ParseException (java.text.ParseException)20 ParsePosition (java.text.ParsePosition)13 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)12 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)11 FieldPosition (java.text.FieldPosition)9 IOException (java.io.IOException)7 DateFormat (android.icu.text.DateFormat)5 Calendar (android.icu.util.Calendar)5 Date (java.util.Date)5 BigDecimal (android.icu.math.BigDecimal)4 DisplayContext (android.icu.text.DisplayContext)4 MeasureFormat (android.icu.text.MeasureFormat)4 SimpleDateFormat (android.icu.text.SimpleDateFormat)4 PluralFormat (android.icu.text.PluralFormat)3