Search in sources :

Example 81 with NumberFormat

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

the class MyNumberFormat method Test4110936.

/**
 * DecimalFormat.setMaximumIntegerDigits() works incorrectly.
 */
@Test
public void Test4110936() {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumIntegerDigits(128);
    logln("setMaximumIntegerDigits(128)");
    if (nf.getMaximumIntegerDigits() != 128)
        errln("getMaximumIntegerDigits() returns " + nf.getMaximumIntegerDigits());
}
Also used : NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 82 with NumberFormat

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

the class MyNumberFormat method Test4071859.

/**
 * Data rounding errors for Italian locale number formats
 * Note- with the Euro, there is no need for currency rounding anymore
 */
@Test
public void Test4071859() {
    NumberFormat formatter;
    String tempString;
    /* user error :
        String expectedDefault = "-5.789,987";
        String expectedCurrency = "-L.\u00a05.789,98";
        String expectedPercent = "-578.998%";
        */
    String expectedDefault = "-5.789,988";
    String expectedCurrency = "-5.789,99\u00A0" + EURO;
    String expectedPercent = "-578.999%";
    formatter = NumberFormat.getNumberInstance(Locale.ITALY);
    tempString = formatter.format(-5789.9876);
    if (tempString.equals(expectedDefault)) {
        logln("Bug 4071859 default test passed.");
    } else {
        errln("a) Failed:" + " Expected " + expectedDefault + " Received " + tempString);
    }
    formatter = NumberFormat.getCurrencyInstance(Locale.ITALY);
    tempString = formatter.format(-5789.9876);
    if (tempString.equals(expectedCurrency)) {
        logln("Bug 4071859 currency test passed.");
    } else {
        errln("b) Failed:" + " Expected " + expectedCurrency + " Received " + tempString);
    }
    formatter = NumberFormat.getPercentInstance(Locale.ITALY);
    tempString = formatter.format(-5789.9876);
    if (tempString.equals(expectedPercent)) {
        logln("Bug 4071859 percentage test passed.");
    } else {
        errln("c) Failed:" + " Expected " + expectedPercent + " Received " + tempString);
    }
}
Also used : NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 83 with NumberFormat

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

the class MyNumberFormat method Test4212072.

/**
 * Some DecimalFormatSymbols changes are not picked up by DecimalFormat.
 * This includes the minus sign, currency symbol, international currency
 * symbol, percent, and permille.  This is filed as bugs 4212072 and
 * 4212073.
 */
// Android-changed: Added @Ignore to suppress the test; it consumes a lot of heap, permanently,
// affecting later tests. http://b/62374714
@Ignore
@Test
public void Test4212072() throws IOException, ClassNotFoundException {
    DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
    DecimalFormat fmt = new DecimalFormat("#", sym);
    sym.setMinusSign('^');
    fmt.setDecimalFormatSymbols(sym);
    if (!fmt.format(-1).equals("^1")) {
        errln("FAIL: -1 x (minus=^) -> " + fmt.format(-1) + ", exp ^1");
    }
    if (!fmt.getNegativePrefix().equals("^")) {
        errln("FAIL: (minus=^).getNegativePrefix -> " + fmt.getNegativePrefix() + ", exp ^");
    }
    sym.setMinusSign('-');
    fmt.applyPattern("#%");
    sym.setPercent('^');
    fmt.setDecimalFormatSymbols(sym);
    if (!fmt.format(0.25).equals("25^")) {
        errln("FAIL: 0.25 x (percent=^) -> " + fmt.format(0.25) + ", exp 25^");
    }
    if (!fmt.getPositiveSuffix().equals("^")) {
        errln("FAIL: (percent=^).getPositiveSuffix -> " + fmt.getPositiveSuffix() + ", exp ^");
    }
    sym.setPercent('%');
    fmt.applyPattern("#\u2030");
    sym.setPerMill('^');
    fmt.setDecimalFormatSymbols(sym);
    if (!fmt.format(0.25).equals("250^")) {
        errln("FAIL: 0.25 x (permill=^) -> " + fmt.format(0.25) + ", exp 250^");
    }
    if (!fmt.getPositiveSuffix().equals("^")) {
        errln("FAIL: (permill=^).getPositiveSuffix -> " + fmt.getPositiveSuffix() + ", exp ^");
    }
    sym.setPerMill('\u2030');
    fmt.applyPattern("\u00A4#.00");
    sym.setCurrencySymbol("usd");
    fmt.setDecimalFormatSymbols(sym);
    if (!fmt.format(12.5).equals("usd12.50")) {
        errln("FAIL: 12.5 x (currency=usd) -> " + fmt.format(12.5) + ", exp usd12.50");
    }
    if (!fmt.getPositivePrefix().equals("usd")) {
        errln("FAIL: (currency=usd).getPositivePrefix -> " + fmt.getPositivePrefix() + ", exp usd");
    }
    sym.setCurrencySymbol("$");
    fmt.applyPattern("\u00A4\u00A4#.00");
    sym.setInternationalCurrencySymbol("DOL");
    fmt.setDecimalFormatSymbols(sym);
    if (!fmt.format(12.5).equals("DOL12.50")) {
        errln("FAIL: 12.5 x (intlcurrency=DOL) -> " + fmt.format(12.5) + ", exp DOL12.50");
    }
    if (!fmt.getPositivePrefix().equals("DOL")) {
        errln("FAIL: (intlcurrency=DOL).getPositivePrefix -> " + fmt.getPositivePrefix() + ", exp DOL");
    }
    sym.setInternationalCurrencySymbol("USD");
    if (VersionInfo.ICU_VERSION == VersionInfo.getInstance(2, 2)) {
        // bug in 2.2 that fails this test
        // to be fixed in the later versions
        System.out.println("\n        Test skipped for release 2.2");
        return;
    }
    // Since the pattern logic has changed, make sure that patterns round
    // trip properly.  Test stream in/out integrity too.
    Locale[] avail = NumberFormat.getAvailableLocales();
    for (int i = 0; i < avail.length; ++i) {
        if ((avail[i].getLanguage().equals("ji") || avail[i].getLanguage().equals("bm")) && logKnownIssue("11234", "Symbol roundtrip issues for locales ji, bm")) {
            continue;
        }
        for (int j = 0; j < 3; ++j) {
            NumberFormat nf;
            switch(j) {
                case 0:
                    nf = NumberFormat.getInstance(avail[i]);
                    break;
                case 1:
                    nf = NumberFormat.getCurrencyInstance(avail[i]);
                    break;
                default:
                    nf = NumberFormat.getPercentInstance(avail[i]);
                    break;
            }
            DecimalFormat df = (DecimalFormat) nf;
            // Test toPattern/applyPattern round trip
            String pat = df.toPattern();
            DecimalFormatSymbols symb = new DecimalFormatSymbols(avail[i]);
            DecimalFormat f2 = new DecimalFormat(pat, symb);
            if (!df.equals(f2)) {
                errln("FAIL: " + avail[i] + " #" + j + " -> \"" + pat + "\" -> \"" + f2.toPattern() + '"');
            }
            // Test toLocalizedPattern/applyLocalizedPattern round trip
            pat = df.toLocalizedPattern();
            try {
                f2.applyLocalizedPattern(pat);
                String s1 = f2.format(123456);
                String s2 = df.format(123456);
                if (!s1.equals(s2)) {
                    errln("FAIL: " + avail[i] + " #" + j + " -> localized \"" + s2 + "\" -> \"" + s2 + '"' + " in locale " + df.getLocale(ULocale.ACTUAL_LOCALE));
                }
                if (!df.equals(f2)) {
                    errln("FAIL: " + avail[i] + " #" + j + " -> localized \"" + pat + "\" -> \"" + f2.toLocalizedPattern() + '"' + " in locale " + df.getLocale(ULocale.ACTUAL_LOCALE));
                    errln("s1: " + s1 + " s2: " + s2);
                }
            } catch (IllegalArgumentException ex) {
                errln(ex.getMessage() + " for locale " + df.getLocale(ULocale.ACTUAL_LOCALE));
            }
            // Test writeObject/readObject round trip
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(df);
            oos.flush();
            baos.close();
            byte[] bytes = baos.toByteArray();
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
            f2 = (DecimalFormat) ois.readObject();
            if (!df.equals(f2)) {
                errln("FAIL: Stream in/out " + avail[i] + " -> \"" + pat + "\" -> " + (f2 != null ? ("\"" + f2.toPattern() + '"') : "null"));
            }
        }
    }
    // @since ICU 2.4
    // Make sure that all special characters, when quoted in a suffix or
    // prefix, lose their special meaning.
    char[] SPECIALS = { '0', ',', '.', '\u2030', '%', '#', ';', 'E', '*', '+', '-' };
    sym = new DecimalFormatSymbols(Locale.US);
    for (int j = 0; j < SPECIALS.length; ++j) {
        char special = SPECIALS[j];
        String pat = "'" + special + "'#0'" + special + "'";
        try {
            fmt = new DecimalFormat(pat, sym);
            String pat2 = fmt.toPattern();
            if (!pat.equals(pat2)) {
                errln("FAIL: Pattern \"" + pat + "\" => toPattern() => \"" + pat2 + "\"");
            }
            String s = fmt.format(123);
            String exp = "" + special + "123" + special;
            if (!s.equals(exp)) {
                errln("FAIL: 123 x \"" + pat + "\" => \"" + s + "\", exp \"" + exp + "\"");
            }
        } catch (IllegalArgumentException e) {
            errln("FAIL: Pattern \"" + pat + "\" => " + e.getMessage());
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) NumberFormat(android.icu.text.NumberFormat) ObjectInputStream(java.io.ObjectInputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 84 with NumberFormat

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

the class RbnfTest method TestUndefinedSpellout.

@Test
public void TestUndefinedSpellout() {
    Locale greek = new Locale("el", "", "");
    RuleBasedNumberFormat[] formatters = { new RuleBasedNumberFormat(greek, RuleBasedNumberFormat.SPELLOUT), new RuleBasedNumberFormat(greek, RuleBasedNumberFormat.ORDINAL), new RuleBasedNumberFormat(greek, RuleBasedNumberFormat.DURATION) };
    String[] data = { "0", "1", "15", "20", "23", "73", "88", "100", "106", "127", "200", "579", "1,000", "2,000", "3,004", "4,567", "15,943", "105,000", "2,345,678", "-36", "-36.91215", "234.56789" };
    NumberFormat decFormat = NumberFormat.getInstance(Locale.US);
    for (int j = 0; j < formatters.length; ++j) {
        android.icu.text.NumberFormat formatter = formatters[j];
        logln("formatter[" + j + "]");
        for (int i = 0; i < data.length; ++i) {
            try {
                String result = formatter.format(decFormat.parse(data[i]));
                logln("[" + i + "] " + data[i] + " ==> " + result);
            } catch (Exception e) {
                errln("formatter[" + j + "], data[" + i + "] " + data[i] + " threw exception " + e.getMessage());
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) DisplayContext(android.icu.text.DisplayContext) ParseException(java.text.ParseException) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 85 with NumberFormat

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

the class RbnfTest method doTest.

void doTest(RuleBasedNumberFormat formatter, String[][] testData, boolean testParsing) {
    // NumberFormat decFmt = NumberFormat.getInstance(Locale.US);
    NumberFormat decFmt = new DecimalFormat("#,###.################");
    try {
        for (int i = 0; i < testData.length; i++) {
            String number = testData[i][0];
            String expectedWords = testData[i][1];
            if (isVerbose()) {
                logln("test[" + i + "] number: " + number + " target: " + expectedWords);
            }
            Number num = decFmt.parse(number);
            String actualWords = formatter.format(num);
            if (!actualWords.equals(expectedWords)) {
                errln("Spot check format failed: for " + number + ", expected\n    " + expectedWords + ", but got\n    " + actualWords);
            } else if (testParsing) {
                String actualNumber = decFmt.format(formatter.parse(actualWords));
                if (!actualNumber.equals(number)) {
                    errln("Spot check parse failed: for " + actualWords + ", expected " + number + ", but got " + actualNumber);
                }
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
        errln("Test failed with exception: " + e.toString());
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat)

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