Search in sources :

Example 56 with NumberFormat

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

the class TestMessageFormat method TestDateFormatHashCode.

public void TestDateFormatHashCode() {
    DateFormat testDF = DateFormat.getDateInstance(DateFormat.DEFAULT, ULocale.GERMAN);
    NumberFormat testNF = testDF.getNumberFormat();
    int expectedResult = testNF.getMaximumIntegerDigits() * 37 + testNF.getMaximumFractionDigits();
    int actualHashResult = testDF.hashCode();
    assertEquals("DateFormat hashCode", expectedResult, actualHashResult);
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) NumberFormat(android.icu.text.NumberFormat)

Example 57 with NumberFormat

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

the class TimeUnitTest method TestGreekWithSanitization.

/**
 * @bug9042
 * Performs tests for Greek.
 * This tests that if the plural count listed in time unit format does not
 * match those in the plural rules for the locale, those plural count in
 * time unit format will be ingored and subsequently, fall back will kick in
 * which is tested above.
 * Without data sanitization, setNumberFormat() would crash.
 * As of CLDR shiped in ICU4.8, Greek is one such language.
 */
@Test
public void TestGreekWithSanitization() {
    ULocale loc = new ULocale("el");
    NumberFormat numfmt = NumberFormat.getInstance(loc);
    TimeUnitFormat tuf = new TimeUnitFormat(loc);
    tuf.parseObject("", new ParsePosition(0));
    tuf.setNumberFormat(numfmt);
}
Also used : ULocale(android.icu.util.ULocale) TimeUnitFormat(android.icu.text.TimeUnitFormat) NumberFormat(android.icu.text.NumberFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 58 with NumberFormat

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

the class TimeUnitTest method Test10219FractionalPlurals.

@Test
public void Test10219FractionalPlurals() {
    TimeUnitFormat tuf = new TimeUnitFormat(ULocale.ENGLISH, TimeUnitFormat.FULL_NAME);
    String[] expected = { "1 minute", "1.5 minutes", "1.58 minutes" };
    for (int i = 2; i >= 0; i--) {
        NumberFormat nf = NumberFormat.getNumberInstance(ULocale.ENGLISH);
        nf.setRoundingMode(BigDecimal.ROUND_DOWN);
        nf.setMaximumFractionDigits(i);
        tuf.setNumberFormat(nf);
        assertEquals("Test10219", expected[i], tuf.format(new TimeUnitAmount(1.588, TimeUnit.MINUTE)));
    }
}
Also used : TimeUnitAmount(android.icu.util.TimeUnitAmount) TimeUnitFormat(android.icu.text.TimeUnitFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 59 with NumberFormat

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

the class WriteNumberFormatSerialTestData method main.

public static void main(String[] args) {
    NumberFormat nf = NumberFormat.getInstance(Locale.US);
    NumberFormat nfc = NumberFormat.getCurrencyInstance(Locale.US);
    NumberFormat nfp = NumberFormat.getPercentInstance(Locale.US);
    NumberFormat nfsp = NumberFormat.getScientificInstance(Locale.US);
    try {
        FileOutputStream file = new FileOutputStream("NumberFormatSerialTestData.java");
        file.write(header.getBytes());
        write(file, (Object) nf, "generalInstance", "//NumberFormat.getInstance(Locale.US)");
        write(file, (Object) nfc, "currencyInstance", "//NumberFormat.getCurrencyInstance(Locale.US)");
        write(file, (Object) nfp, "percentInstance", "//NumberFormat.getPercentInstance(Locale.US)");
        write(file, (Object) nfsp, "scientificInstance", "//NumberFormat.getScientificInstance(Locale.US)");
        file.write(footer.getBytes());
        file.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) NumberFormat(android.icu.text.NumberFormat)

Example 60 with NumberFormat

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

the class RelativeDateTimeFormatterTest method TestCustomNumberFormat.

@Test
public void TestCustomNumberFormat() {
    ULocale loc = new ULocale("en_US");
    NumberFormat nf = NumberFormat.getInstance(loc);
    nf.setMinimumFractionDigits(1);
    nf.setMaximumFractionDigits(1);
    RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(loc, nf);
    // Change nf after the fact to prove that we made a defensive copy
    nf.setMinimumFractionDigits(3);
    nf.setMaximumFractionDigits(3);
    // Change getNumberFormat to prove we made defensive copy going out.
    fmt.getNumberFormat().setMinimumFractionDigits(5);
    assertEquals("TestCustomNumberformat", 1, fmt.getNumberFormat().getMinimumFractionDigits());
    Object[][] data = { { 0.0, Direction.NEXT, RelativeUnit.SECONDS, "in 0.0 seconds" }, { 0.5, Direction.NEXT, RelativeUnit.SECONDS, "in 0.5 seconds" }, { 1.0, Direction.NEXT, RelativeUnit.SECONDS, "in 1.0 seconds" }, { 2.0, Direction.NEXT, RelativeUnit.SECONDS, "in 2.0 seconds" } };
    for (Object[] row : data) {
        String actual = fmt.format(((Double) row[0]).doubleValue(), (Direction) row[1], (RelativeUnit) row[2]);
        assertEquals("Relative date with quantity special NumberFormat", row[3], actual);
    }
}
Also used : ULocale(android.icu.util.ULocale) RelativeDateTimeFormatter(android.icu.text.RelativeDateTimeFormatter) 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