Search in sources :

Example 26 with NumberFormat

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

the class NumberFormatTest method TestParseCurrency.

@Test
public void TestParseCurrency() {
    class ParseCurrencyItem {

        private final String localeString;

        private final String descrip;

        private final String currStr;

        private final int numExpectPos;

        private final int numExpectVal;

        private final int curExpectPos;

        private final int curExpectVal;

        private final String curExpectCurr;

        ParseCurrencyItem(String locStr, String desc, String curr, int numExPos, int numExVal, int curExPos, int curExVal, String curExCurr) {
            localeString = locStr;
            descrip = desc;
            currStr = curr;
            numExpectPos = numExPos;
            numExpectVal = numExVal;
            curExpectPos = curExPos;
            curExpectVal = curExVal;
            curExpectCurr = curExCurr;
        }

        public String getLocaleString() {
            return localeString;
        }

        public String getDescrip() {
            return descrip;
        }

        public String getCurrStr() {
            return currStr;
        }

        public int getNumExpectPos() {
            return numExpectPos;
        }

        public int getNumExpectVal() {
            return numExpectVal;
        }

        public int getCurExpectPos() {
            return curExpectPos;
        }

        public int getCurExpectVal() {
            return curExpectVal;
        }

        public String getCurExpectCurr() {
            return curExpectCurr;
        }
    }
    final ParseCurrencyItem[] parseCurrencyItems = { new ParseCurrencyItem("en_US", "dollars2", "$2.00", 5, 2, 5, 2, "USD"), new ParseCurrencyItem("en_US", "dollars4", "$4", 2, 4, 2, 4, "USD"), new ParseCurrencyItem("en_US", "dollars9", "9\u00A0$", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_US", "pounds3", "\u00A33.00", 0, 0, 5, 3, "GBP"), new ParseCurrencyItem("en_US", "pounds5", "\u00A35", 0, 0, 2, 5, "GBP"), new ParseCurrencyItem("en_US", "pounds7", "7\u00A0\u00A3", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_US", "euros8", "\u20AC8", 0, 0, 2, 8, "EUR"), new ParseCurrencyItem("en_GB", "pounds3", "\u00A33.00", 5, 3, 5, 3, "GBP"), new ParseCurrencyItem("en_GB", "pounds5", "\u00A35", 2, 5, 2, 5, "GBP"), new ParseCurrencyItem("en_GB", "pounds7", "7\u00A0\u00A3", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_GB", "euros4", "4,00\u00A0\u20AC", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_GB", "euros6", "6\u00A0\u20AC", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_GB", "euros8", "\u20AC8", 0, 0, 2, 8, "EUR"), new ParseCurrencyItem("en_GB", "dollars4", "US$4", 0, 0, 4, 4, "USD"), new ParseCurrencyItem("fr_FR", "euros4", "4,00\u00A0\u20AC", 6, 4, 6, 4, "EUR"), new ParseCurrencyItem("fr_FR", "euros6", "6\u00A0\u20AC", 3, 6, 3, 6, "EUR"), new ParseCurrencyItem("fr_FR", "euros8", "\u20AC8", 0, 0, 0, 0, ""), new ParseCurrencyItem("fr_FR", "dollars2", "$2.00", 0, 0, 0, 0, ""), new ParseCurrencyItem("fr_FR", "dollars4", "$4", 0, 0, 0, 0, "") };
    for (ParseCurrencyItem item : parseCurrencyItems) {
        String localeString = item.getLocaleString();
        ULocale uloc = new ULocale(localeString);
        NumberFormat fmt = null;
        try {
            fmt = NumberFormat.getCurrencyInstance(uloc);
        } catch (Exception e) {
            errln("NumberFormat.getCurrencyInstance fails for locale " + localeString);
            continue;
        }
        String currStr = item.getCurrStr();
        ParsePosition parsePos = new ParsePosition(0);
        Number numVal = fmt.parse(currStr, parsePos);
        if (parsePos.getIndex() != item.getNumExpectPos() || (numVal != null && numVal.intValue() != item.getNumExpectVal())) {
            if (numVal != null) {
                errln("NumberFormat.getCurrencyInstance parse " + localeString + "/" + item.getDescrip() + ", expect pos/val " + item.getNumExpectPos() + "/" + item.getNumExpectVal() + ", get " + parsePos.getIndex() + "/" + numVal.intValue());
            } else {
                errln("NumberFormat.getCurrencyInstance parse " + localeString + "/" + item.getDescrip() + ", expect pos/val " + item.getNumExpectPos() + "/" + item.getNumExpectVal() + ", get " + parsePos.getIndex() + "/(NULL)");
            }
        }
        parsePos.setIndex(0);
        CurrencyAmount currAmt = fmt.parseCurrency(currStr, parsePos);
        if (parsePos.getIndex() != item.getCurExpectPos() || (currAmt != null && (currAmt.getNumber().intValue() != item.getCurExpectVal() || currAmt.getCurrency().getCurrencyCode().compareTo(item.getCurExpectCurr()) != 0))) {
            if (currAmt != null) {
                errln("NumberFormat.getCurrencyInstance parseCurrency " + localeString + "/" + item.getDescrip() + ", expect pos/val/curr " + item.getCurExpectPos() + "/" + item.getCurExpectVal() + "/" + item.getCurExpectCurr() + ", get " + parsePos.getIndex() + "/" + currAmt.getNumber().intValue() + "/" + currAmt.getCurrency().getCurrencyCode());
            } else {
                errln("NumberFormat.getCurrencyInstance parseCurrency " + localeString + "/" + item.getDescrip() + ", expect pos/val/curr " + item.getCurExpectPos() + "/" + item.getCurExpectVal() + "/" + item.getCurExpectCurr() + ", get " + parsePos.getIndex() + "/(NULL)");
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) ParseException(java.text.ParseException) IOException(java.io.IOException) CurrencyAmount(android.icu.util.CurrencyAmount) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 27 with NumberFormat

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

the class NumberFormatTest method TestRounding487.

/**
 * Test proper rounding by the format method.
 */
@Test
public void TestRounding487() {
    NumberFormat nf = NumberFormat.getInstance();
    roundingTest(nf, 0.00159999, 4, "0.0016");
    roundingTest(nf, 0.00995, 4, "0.01");
    roundingTest(nf, 12.3995, 3, "12.4");
    roundingTest(nf, 12.4999, 0, "12");
    roundingTest(nf, -19.5, 0, "-20");
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 28 with NumberFormat

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

the class NumberFormatTest method TestSetMinimumIntegerDigits.

/*
     * Tests the method public void setMinimumIntegerDigits(int newValue)
     */
@Test
public void TestSetMinimumIntegerDigits() {
    NumberFormat nf = NumberFormat.getInstance();
    // For valid array, it is displayed as {min value, max value}
    // Tests when "if (minimumIntegerDigits > maximumIntegerDigits)" is true
    int[][] cases = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 2, 0 }, { 2, 1 }, { 10, 0 } };
    int[] expectedMax = { 0, 1, 1, 2, 2, 10 };
    if (cases.length != expectedMax.length) {
        errln("Can't continue test case method TestSetMinimumIntegerDigits " + "since the test case arrays are unequal.");
    } else {
        for (int i = 0; i < cases.length; i++) {
            nf.setMaximumIntegerDigits(cases[i][1]);
            nf.setMinimumIntegerDigits(cases[i][0]);
            if (nf.getMaximumIntegerDigits() != expectedMax[i]) {
                errln("NumberFormat.setMinimumIntegerDigits(int newValue " + "did not return an expected result for parameter " + cases[i][1] + " and " + cases[i][0] + " and expected " + expectedMax[i] + " but got " + nf.getMaximumIntegerDigits());
            }
        }
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 29 with NumberFormat

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

the class NumberFormatTest method TestCurrency.

/**
 * Test localized currency patterns.
 */
@Test
public void TestCurrency() {
    String[] DATA = { "fr", "CA", "", "1,50\u00a0$", "de", "DE", "", "1,50\u00a0\u20AC", "de", "DE", "PREEURO", "1,50\u00a0DM", "fr", "FR", "", "1,50\u00a0\u20AC", "fr", "FR", "PREEURO", "1,50\u00a0F" };
    for (int i = 0; i < DATA.length; i += 4) {
        Locale locale = new Locale(DATA[i], DATA[i + 1], DATA[i + 2]);
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        String s = fmt.format(1.50);
        if (s.equals(DATA[i + 3])) {
            logln("Ok: 1.50 x " + locale + " => " + s);
        } else {
            logln("FAIL: 1.50 x " + locale + " => " + s + ", expected " + DATA[i + 3]);
        }
    }
    // format currency with CurrencyAmount
    for (int i = 0; i < DATA.length; i += 4) {
        Locale locale = new Locale(DATA[i], DATA[i + 1], DATA[i + 2]);
        Currency curr = Currency.getInstance(locale);
        logln("\nName of the currency is: " + curr.getName(locale, Currency.LONG_NAME, new boolean[] { false }));
        CurrencyAmount cAmt = new CurrencyAmount(1.5, curr);
        // cover hashCode
        logln("CurrencyAmount object's hashCode is: " + cAmt.hashCode());
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        String sCurr = fmt.format(cAmt);
        if (sCurr.equals(DATA[i + 3])) {
            logln("Ok: 1.50 x " + locale + " => " + sCurr);
        } else {
            errln("FAIL: 1.50 x " + locale + " => " + sCurr + ", expected " + DATA[i + 3]);
        }
    }
    // Cover MeasureFormat.getCurrencyFormat()
    ULocale save = ULocale.getDefault();
    ULocale.setDefault(ULocale.US);
    MeasureFormat curFmt = MeasureFormat.getCurrencyFormat();
    String strBuf = curFmt.format(new CurrencyAmount(new Float(1234.56), Currency.getInstance("USD")));
    try {
        CurrencyAmount parsedVal = (CurrencyAmount) curFmt.parseObject(strBuf);
        Number val = parsedVal.getNumber();
        if (!val.equals(new BigDecimal("1234.56"))) {
            errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the number. val=" + val);
        }
        if (!parsedVal.getCurrency().equals(Currency.getInstance("USD"))) {
            errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the currency");
        }
    } catch (ParseException e) {
        errln("FAIL: " + e.getMessage());
    }
    ULocale.setDefault(save);
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ULocale(android.icu.util.ULocale) CurrencyAmount(android.icu.util.CurrencyAmount) BigDecimal(android.icu.math.BigDecimal) Currency(android.icu.util.Currency) ParseException(java.text.ParseException) MeasureFormat(android.icu.text.MeasureFormat) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 30 with NumberFormat

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

the class MeasureUnitTest method TestFormatPeriodEn.

@Test
public void TestFormatPeriodEn() {
    TimeUnitAmount[] _19m = { new TimeUnitAmount(19.0, TimeUnit.MINUTE) };
    TimeUnitAmount[] _1h_23_5s = { new TimeUnitAmount(1.0, TimeUnit.HOUR), new TimeUnitAmount(23.5, TimeUnit.SECOND) };
    TimeUnitAmount[] _1h_23_5m = { new TimeUnitAmount(1.0, TimeUnit.HOUR), new TimeUnitAmount(23.5, TimeUnit.MINUTE) };
    TimeUnitAmount[] _1h_0m_23s = { new TimeUnitAmount(1.0, TimeUnit.HOUR), new TimeUnitAmount(0.0, TimeUnit.MINUTE), new TimeUnitAmount(23.0, TimeUnit.SECOND) };
    TimeUnitAmount[] _2y_5M_3w_4d = { new TimeUnitAmount(2.0, TimeUnit.YEAR), new TimeUnitAmount(5.0, TimeUnit.MONTH), new TimeUnitAmount(3.0, TimeUnit.WEEK), new TimeUnitAmount(4.0, TimeUnit.DAY) };
    TimeUnitAmount[] _1m_59_9996s = { new TimeUnitAmount(1.0, TimeUnit.MINUTE), new TimeUnitAmount(59.9996, TimeUnit.SECOND) };
    TimeUnitAmount[] _5h_17m = { new TimeUnitAmount(5.0, TimeUnit.HOUR), new TimeUnitAmount(17.0, TimeUnit.MINUTE) };
    TimeUnitAmount[] _neg5h_17m = { new TimeUnitAmount(-5.0, TimeUnit.HOUR), new TimeUnitAmount(17.0, TimeUnit.MINUTE) };
    TimeUnitAmount[] _19m_28s = { new TimeUnitAmount(19.0, TimeUnit.MINUTE), new TimeUnitAmount(28.0, TimeUnit.SECOND) };
    TimeUnitAmount[] _0h_0m_9s = { new TimeUnitAmount(0.0, TimeUnit.HOUR), new TimeUnitAmount(0.0, TimeUnit.MINUTE), new TimeUnitAmount(9.0, TimeUnit.SECOND) };
    TimeUnitAmount[] _0h_0m_17s = { new TimeUnitAmount(0.0, TimeUnit.HOUR), new TimeUnitAmount(0.0, TimeUnit.MINUTE), new TimeUnitAmount(17.0, TimeUnit.SECOND) };
    TimeUnitAmount[] _6h_56_92m = { new TimeUnitAmount(6.0, TimeUnit.HOUR), new TimeUnitAmount(56.92, TimeUnit.MINUTE) };
    TimeUnitAmount[] _3h_4s_5m = { new TimeUnitAmount(3.0, TimeUnit.HOUR), new TimeUnitAmount(4.0, TimeUnit.SECOND), new TimeUnitAmount(5.0, TimeUnit.MINUTE) };
    TimeUnitAmount[] _6_7h_56_92m = { new TimeUnitAmount(6.7, TimeUnit.HOUR), new TimeUnitAmount(56.92, TimeUnit.MINUTE) };
    TimeUnitAmount[] _3h_5h = { new TimeUnitAmount(3.0, TimeUnit.HOUR), new TimeUnitAmount(5.0, TimeUnit.HOUR) };
    Object[][] fullData = { { _1m_59_9996s, "1 minute, 59.9996 seconds" }, { _19m, "19 minutes" }, { _1h_23_5s, "1 hour, 23.5 seconds" }, { _1h_23_5m, "1 hour, 23.5 minutes" }, { _1h_0m_23s, "1 hour, 0 minutes, 23 seconds" }, { _2y_5M_3w_4d, "2 years, 5 months, 3 weeks, 4 days" } };
    Object[][] abbrevData = { { _1m_59_9996s, "1 min, 59.9996 sec" }, { _19m, "19 min" }, { _1h_23_5s, "1 hr, 23.5 sec" }, { _1h_23_5m, "1 hr, 23.5 min" }, { _1h_0m_23s, "1 hr, 0 min, 23 sec" }, { _2y_5M_3w_4d, "2 yrs, 5 mths, 3 wks, 4 days" } };
    Object[][] narrowData = { { _1m_59_9996s, "1m 59.9996s" }, { _19m, "19m" }, { _1h_23_5s, "1h 23.5s" }, { _1h_23_5m, "1h 23.5m" }, { _1h_0m_23s, "1h 0m 23s" }, { _2y_5M_3w_4d, "2y 5m 3w 4d" } };
    Object[][] numericData = { { _1m_59_9996s, "1:59.9996" }, { _19m, "19m" }, { _1h_23_5s, "1:00:23.5" }, { _1h_0m_23s, "1:00:23" }, { _1h_23_5m, "1:23.5" }, { _5h_17m, "5:17" }, { _neg5h_17m, "-5h 17m" }, { _19m_28s, "19:28" }, { _2y_5M_3w_4d, "2y 5m 3w 4d" }, { _0h_0m_9s, "0:00:09" }, { _6h_56_92m, "6:56.92" }, { _6_7h_56_92m, "6:56.92" }, { _3h_4s_5m, "3h 4s 5m" }, { _3h_5h, "3h 5h" } };
    Object[][] fullDataDe = { { _1m_59_9996s, "1 Minute, 59,9996 Sekunden" }, { _19m, "19 Minuten" }, { _1h_23_5s, "1 Stunde, 23,5 Sekunden" }, { _1h_23_5m, "1 Stunde, 23,5 Minuten" }, { _1h_0m_23s, "1 Stunde, 0 Minuten und 23 Sekunden" }, { _2y_5M_3w_4d, "2 Jahre, 5 Monate, 3 Wochen und 4 Tage" } };
    Object[][] numericDataDe = { { _1m_59_9996s, "1:59,9996" }, { _19m, "19 Min." }, { _1h_23_5s, "1:00:23,5" }, { _1h_0m_23s, "1:00:23" }, { _1h_23_5m, "1:23,5" }, { _5h_17m, "5:17" }, { _19m_28s, "19:28" }, { _2y_5M_3w_4d, "2 J, 5 M, 3 W und 4 T" }, { _0h_0m_17s, "0:00:17" }, { _6h_56_92m, "6:56,92" }, { _3h_5h, "3 Std., 5 Std." } };
    NumberFormat nf = NumberFormat.getNumberInstance(ULocale.ENGLISH);
    nf.setMaximumFractionDigits(4);
    MeasureFormat mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.WIDE, nf);
    verifyFormatPeriod("en FULL", mf, fullData);
    mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.SHORT, nf);
    verifyFormatPeriod("en SHORT", mf, abbrevData);
    mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.NARROW, nf);
    verifyFormatPeriod("en NARROW", mf, narrowData);
    mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.NUMERIC, nf);
    verifyFormatPeriod("en NUMERIC", mf, numericData);
    nf = NumberFormat.getNumberInstance(ULocale.GERMAN);
    nf.setMaximumFractionDigits(4);
    mf = MeasureFormat.getInstance(ULocale.GERMAN, FormatWidth.WIDE, nf);
    verifyFormatPeriod("de FULL", mf, fullDataDe);
    mf = MeasureFormat.getInstance(ULocale.GERMAN, FormatWidth.NUMERIC, nf);
    verifyFormatPeriod("de NUMERIC", mf, numericDataDe);
    // Same tests, with Java Locale
    nf = NumberFormat.getNumberInstance(Locale.GERMAN);
    nf.setMaximumFractionDigits(4);
    mf = MeasureFormat.getInstance(Locale.GERMAN, FormatWidth.WIDE, nf);
    verifyFormatPeriod("de FULL(Java Locale)", mf, fullDataDe);
    mf = MeasureFormat.getInstance(Locale.GERMAN, FormatWidth.NUMERIC, nf);
    verifyFormatPeriod("de NUMERIC(Java Locale)", mf, numericDataDe);
}
Also used : TimeUnitAmount(android.icu.util.TimeUnitAmount) MeasureFormat(android.icu.text.MeasureFormat) 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