Search in sources :

Example 6 with MeasureFormat

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

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

the class MeasureUnitTest method testParseObject.

@Test
public void testParseObject() {
    MeasureFormat mf = MeasureFormat.getInstance(Locale.GERMAN, FormatWidth.NARROW);
    try {
        mf.parseObject("3m", null);
        fail("MeasureFormat.parseObject(String, ParsePosition) " + "should throw an UnsupportedOperationException");
    } catch (UnsupportedOperationException expected) {
    }
}
Also used : MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Example 8 with MeasureFormat

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

the class MeasureUnitTest method TestExamplesInDocs.

@Test
public void TestExamplesInDocs() {
    MeasureFormat fmtFr = MeasureFormat.getInstance(ULocale.FRENCH, FormatWidth.SHORT);
    Measure measure = new Measure(23, MeasureUnit.CELSIUS);
    assertEquals("23 °C", "23 °C", fmtFr.format(measure));
    Measure measureF = new Measure(70, MeasureUnit.FAHRENHEIT);
    assertEquals("70 °F", "70 °F", fmtFr.format(measureF));
    MeasureFormat fmtFrFull = MeasureFormat.getInstance(ULocale.FRENCH, FormatWidth.WIDE);
    assertEquals("70 pied et 5,3 pouces", "70 pieds et 5,3 pouces", fmtFrFull.formatMeasures(new Measure(70, MeasureUnit.FOOT), new Measure(5.3, MeasureUnit.INCH)));
    assertEquals("1 pied et 1 pouce", "1 pied et 1 pouce", fmtFrFull.formatMeasures(new Measure(1, MeasureUnit.FOOT), new Measure(1, MeasureUnit.INCH)));
    MeasureFormat fmtFrNarrow = MeasureFormat.getInstance(ULocale.FRENCH, FormatWidth.NARROW);
    assertEquals("1′ 1″", "1′ 1″", fmtFrNarrow.formatMeasures(new Measure(1, MeasureUnit.FOOT), new Measure(1, MeasureUnit.INCH)));
    MeasureFormat fmtEn = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.WIDE);
    assertEquals("1 inch, 2 feet", "1 inch, 2 feet", fmtEn.formatMeasures(new Measure(1, MeasureUnit.INCH), new Measure(2, MeasureUnit.FOOT)));
}
Also used : Measure(android.icu.util.Measure) MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Example 9 with MeasureFormat

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

Example 10 with MeasureFormat

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

the class MeasureUnitTest method testEqHashCode.

@Test
public void testEqHashCode() {
    MeasureFormat mf = MeasureFormat.getInstance(ULocale.CANADA, FormatWidth.SHORT);
    MeasureFormat mfeq = MeasureFormat.getInstance(ULocale.CANADA, FormatWidth.SHORT);
    MeasureFormat mfne = MeasureFormat.getInstance(ULocale.CANADA, FormatWidth.WIDE);
    MeasureFormat mfne2 = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.SHORT);
    verifyEqualsHashCode(mf, mfeq, mfne);
    verifyEqualsHashCode(mf, mfeq, mfne2);
}
Also used : MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Aggregations

MeasureFormat (android.icu.text.MeasureFormat)38 Test (org.junit.Test)32 Measure (android.icu.util.Measure)26 ULocale (android.icu.util.ULocale)11 Locale (java.util.Locale)8 ArrayList (java.util.ArrayList)7 SpannableStringBuilder (android.text.SpannableStringBuilder)6 TtsSpan (android.text.style.TtsSpan)6 FormatWidth (android.icu.text.MeasureFormat.FormatWidth)5 NumberFormat (android.icu.text.NumberFormat)5 FieldPosition (java.text.FieldPosition)5 MeasureUnit (android.icu.util.MeasureUnit)4 CurrencyAmount (android.icu.util.CurrencyAmount)3 ParseException (java.text.ParseException)3 BigDecimal (android.icu.math.BigDecimal)2 IOException (java.io.IOException)2 ResourceReader (android.icu.impl.data.ResourceReader)1 TokenIterator (android.icu.impl.data.TokenIterator)1 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)1 DecimalFormat (android.icu.text.DecimalFormat)1