Search in sources :

Example 86 with NumberFormat

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

the class RelativeDateTimeFormatterTest method TestRelativeDateTimeUnitFormatters.

@Test
public void TestRelativeDateTimeUnitFormatters() {
    double[] offsets = { -5.0, -2.2, -2.0, -1.0, -0.7, 0.0, 0.7, 1.0, 2.0, 5.0 };
    String[] en_decDef_long_midSent_week = { /*  text                    numeric */
    "5 weeks ago", "5 weeks ago", /* -5   */
    "2.2 weeks ago", "2.2 weeks ago", /* -2.2 */
    "2 weeks ago", "2 weeks ago", /* -2   */
    "last week", "1 week ago", /* -1   */
    "0.7 weeks ago", "0.7 weeks ago", /* -0.7 */
    "this week", "in 0 weeks", /*  0   */
    "in 0.7 weeks", "in 0.7 weeks", /*  0.7 */
    "next week", "in 1 week", /*  1   */
    "in 2 weeks", "in 2 weeks", /*  2   */
    "in 5 weeks", "in 5 weeks" /*  5   */
    };
    String[] en_dec0_long_midSent_week = { /*  text                    numeric */
    "5 weeks ago", "5 weeks ago", /* -5   */
    "2 weeks ago", "2 weeks ago", /* -2.2 */
    "2 weeks ago", "2 weeks ago", /* -2   */
    "last week", "1 week ago", /* -1   */
    "0 weeks ago", "0 weeks ago", /* -0.7 */
    "this week", "in 0 weeks", /*  0   */
    "in 0 weeks", "in 0 weeks", /*  0.7 */
    "next week", "in 1 week", /*  1   */
    "in 2 weeks", "in 2 weeks", /*  2   */
    "in 5 weeks", "in 5 weeks" /*  5   */
    };
    String[] en_decDef_short_midSent_week = { /*  text                    numeric */
    "5 wk. ago", "5 wk. ago", /* -5   */
    "2.2 wk. ago", "2.2 wk. ago", /* -2.2 */
    "2 wk. ago", "2 wk. ago", /* -2   */
    "last wk.", "1 wk. ago", /* -1   */
    "0.7 wk. ago", "0.7 wk. ago", /* -0.7 */
    "this wk.", "in 0 wk.", /*  0   */
    "in 0.7 wk.", "in 0.7 wk.", /*  0.7 */
    "next wk.", "in 1 wk.", /*  1   */
    "in 2 wk.", "in 2 wk.", /*  2   */
    "in 5 wk.", "in 5 wk." /*  5   */
    };
    String[] en_decDef_long_midSent_min = { /*  text                    numeric */
    "5 minutes ago", "5 minutes ago", /* -5   */
    "2.2 minutes ago", "2.2 minutes ago", /* -2.2 */
    "2 minutes ago", "2 minutes ago", /* -2   */
    "1 minute ago", "1 minute ago", /* -1   */
    "0.7 minutes ago", "0.7 minutes ago", /* -0.7 */
    "in 0 minutes", "in 0 minutes", /*  0   */
    "in 0.7 minutes", "in 0.7 minutes", /*  0.7 */
    "in 1 minute", "in 1 minute", /*  1   */
    "in 2 minutes", "in 2 minutes", /*  2   */
    "in 5 minutes", "in 5 minutes" /*  5   */
    };
    String[] en_dec0_long_midSent_tues = { /*  text                    numeric */
    "", /*no data */
    "", /* -5   */
    "", /*no data */
    "", /* -2.2 */
    "", /*no data */
    "", /* -2   */
    "last Tuesday", "", /* -1   */
    "", /*no data */
    "", /* -0.7 */
    "this Tuesday", "", /*  0   */
    "", /*no data */
    "", /*  0.7 */
    "next Tuesday", "", /*  1   */
    "", /*no data */
    "", /*  2   */
    "", /*no data */
    "" /*no data */
    /*  5   */
    };
    String[] fr_decDef_long_midSent_day = { /*  text                    numeric */
    "il y a 5 jours", "il y a 5 jours", /* -5   */
    "il y a 2,2 jours", "il y a 2,2 jours", /* -2.2 */
    "avant-hier", "il y a 2 jours", /* -2   */
    "hier", "il y a 1 jour", /* -1   */
    "il y a 0,7 jour", "il y a 0,7 jour", /* -0.7 */
    "aujourd’hui", "dans 0 jour", /*  0   */
    "dans 0,7 jour", "dans 0,7 jour", /*  0.7 */
    "demain", "dans 1 jour", /*  1   */
    "après-demain", "dans 2 jours", /*  2   */
    "dans 5 jours", "dans 5 jours" /*  5   */
    };
    class TestRelativeDateTimeUnitItem {

        public String localeID;

        public int decPlaces;

        /* fixed decimal places; -1 to use default num formatter */
        public Style width;

        public DisplayContext capContext;

        public RelativeDateTimeUnit unit;

        public String[] expectedResults;

        /* for the various offsets */
        public TestRelativeDateTimeUnitItem(String locID, int decP, RelativeDateTimeFormatter.Style wid, DisplayContext capC, RelativeDateTimeUnit ut, String[] expR) {
            localeID = locID;
            decPlaces = decP;
            width = wid;
            capContext = capC;
            unit = ut;
            expectedResults = expR;
        }
    }
    ;
    final TestRelativeDateTimeUnitItem[] items = { new TestRelativeDateTimeUnitItem("en", -1, Style.LONG, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.WEEK, en_decDef_long_midSent_week), new TestRelativeDateTimeUnitItem("en", 0, Style.LONG, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.WEEK, en_dec0_long_midSent_week), new TestRelativeDateTimeUnitItem("en", -1, Style.SHORT, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.WEEK, en_decDef_short_midSent_week), new TestRelativeDateTimeUnitItem("en", -1, Style.LONG, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.MINUTE, en_decDef_long_midSent_min), new TestRelativeDateTimeUnitItem("en", -1, Style.LONG, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.TUESDAY, en_dec0_long_midSent_tues), new TestRelativeDateTimeUnitItem("fr", -1, Style.LONG, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, RelativeDateTimeUnit.DAY, fr_decDef_long_midSent_day) };
    for (TestRelativeDateTimeUnitItem item : items) {
        ULocale uloc = new ULocale(item.localeID);
        NumberFormat nf = null;
        if (item.decPlaces >= 0) {
            nf = NumberFormat.getInstance(uloc, NumberFormat.NUMBERSTYLE);
            nf.setMinimumFractionDigits(item.decPlaces);
            nf.setMaximumFractionDigits(item.decPlaces);
            nf.setRoundingMode(BigDecimal.ROUND_DOWN);
        }
        RelativeDateTimeFormatter reldatefmt = RelativeDateTimeFormatter.getInstance(uloc, nf, item.width, item.capContext);
        for (int iOffset = 0; iOffset < offsets.length; iOffset++) {
            double offset = offsets[iOffset];
            if (item.unit == RelativeDateTimeUnit.TUESDAY && offset != -1.0 && offset != 0.0 && offset != 1.0) {
                continue;
            /* we do not currently have data for this */
            }
            String result = reldatefmt.format(offset, item.unit);
            assertEquals("RelativeDateTimeUnit format locale " + item.localeID + ", dec " + item.decPlaces + ", width " + item.width + ", unit " + item.unit, item.expectedResults[iOffset * 2], result);
            if (item.unit == RelativeDateTimeUnit.TUESDAY) {
                continue;
            /* we do not currently have numeric-style data for this */
            }
            result = reldatefmt.formatNumeric(offset, item.unit);
            assertEquals("RelativeDateTimeUnit formatNum locale " + item.localeID + ", dec " + item.decPlaces + ", width " + item.width + ", unit " + item.unit, item.expectedResults[iOffset * 2 + 1], result);
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DisplayContext(android.icu.text.DisplayContext) RelativeDateTimeUnit(android.icu.text.RelativeDateTimeFormatter.RelativeDateTimeUnit) RelativeDateTimeFormatter(android.icu.text.RelativeDateTimeFormatter) Style(android.icu.text.RelativeDateTimeFormatter.Style) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 87 with NumberFormat

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

the class NumberFormatTest method TestCurrencySign.

/**
 * Test the handling of the currency symbol in patterns.
 */
@Test
public void TestCurrencySign() {
    DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
    StringBuffer pat = new StringBuffer("");
    char currency = 0x00A4;
    // "\xA4#,##0.00;-\xA4#,##0.00"
    pat.append(currency).append("#,##0.00;-").append(currency).append("#,##0.00");
    DecimalFormat fmt = new DecimalFormat(pat.toString(), sym);
    String s = ((NumberFormat) fmt).format(1234.56);
    pat = new StringBuffer();
    logln("Pattern \"" + fmt.toPattern() + "\"");
    logln(" Format " + 1234.56 + " . " + s);
    assertEquals("symbol, pos", "$1,234.56", s);
    s = ((NumberFormat) fmt).format(-1234.56);
    logln(" Format " + Double.toString(-1234.56) + " . " + s);
    assertEquals("symbol, neg", "-$1,234.56", s);
    pat.setLength(0);
    // "\xA4\xA4 #,##0.00;\xA4\xA4 -#,##0.00"
    pat.append(currency).append(currency).append(" #,##0.00;").append(currency).append(currency).append(" -#,##0.00");
    fmt = new DecimalFormat(pat.toString(), sym);
    s = ((NumberFormat) fmt).format(1234.56);
    logln("Pattern \"" + fmt.toPattern() + "\"");
    logln(" Format " + Double.toString(1234.56) + " . " + s);
    assertEquals("name, pos", "USD 1,234.56", s);
    s = ((NumberFormat) fmt).format(-1234.56);
    logln(" Format " + Double.toString(-1234.56) + " . " + s);
    assertEquals("name, neg", "USD -1,234.56", s);
}
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 88 with NumberFormat

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

the class NumberFormatTest method TestParseCurrencyTrailingSymbol.

@Test
public void TestParseCurrencyTrailingSymbol() {
    // see sun bug 4709840
    NumberFormat fmt = NumberFormat.getCurrencyInstance(Locale.GERMANY);
    float val = 12345.67f;
    String str = fmt.format(val);
    logln("val: " + val + " str: " + str);
    try {
        Number num = fmt.parse(str);
        logln("num: " + num);
    } catch (ParseException e) {
        errln("parse of '" + str + "' threw exception: " + e);
    }
}
Also used : ParseException(java.text.ParseException) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 89 with NumberFormat

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

the class NumberFormatTest method Test11621.

// Test that the decimal is shown even when there are no fractional digits
@Test
public void Test11621() throws Exception {
    String pat = "0.##E0";
    DecimalFormatSymbols icuSym = new DecimalFormatSymbols(Locale.US);
    DecimalFormat icuFmt = new DecimalFormat(pat, icuSym);
    icuFmt.setDecimalSeparatorAlwaysShown(true);
    String icu = ((NumberFormat) icuFmt).format(299792458);
    java.text.DecimalFormatSymbols jdkSym = new java.text.DecimalFormatSymbols(Locale.US);
    java.text.DecimalFormat jdkFmt = new java.text.DecimalFormat(pat, jdkSym);
    jdkFmt.setDecimalSeparatorAlwaysShown(true);
    String jdk = ((java.text.NumberFormat) jdkFmt).format(299792458);
    assertEquals("ICU and JDK placement of decimal in exponent", jdk, icu);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) MathContext(android.icu.math.MathContext) DisplayContext(android.icu.text.DisplayContext) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 90 with NumberFormat

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

the class MyNumberFormat method Test4162198.

/**
 * NumberFormat cannot format Double.MAX_VALUE
 */
@Test
public void Test4162198() {
    double dbl = Double.MAX_VALUE;
    NumberFormat f = NumberFormat.getInstance();
    f.setMaximumFractionDigits(Integer.MAX_VALUE);
    f.setMaximumIntegerDigits(Integer.MAX_VALUE);
    String s = f.format(dbl);
    logln("The number " + dbl + " formatted to " + s);
    Number n = null;
    try {
        n = f.parse(s);
    } catch (java.text.ParseException e) {
        errln("Caught a ParseException:");
        e.printStackTrace();
    }
    logln("The string " + s + " parsed as " + n);
    if (n.doubleValue() != dbl) {
        errln("Round trip failure");
    }
}
Also used : ParseException(java.text.ParseException) 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