Java Code Examples for android.icu.text.NumberFormat#setMinimumFractionDigits()

The following examples show how to use android.icu.text.NumberFormat#setMinimumFractionDigits() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MeasureUnitTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void Test10219FractionalPlurals() {
    double[] values = {1.588, 1.011};
    String[][] expected = {
            {"1 minute", "1.5 minutes", "1.58 minutes"},
            {"1 minute", "1.0 minutes", "1.01 minutes"}
    };
    for (int j = 0; j < values.length; j++) {
        for (int i = 0; i < expected[j].length; i++) {
            NumberFormat nf = NumberFormat.getNumberInstance(ULocale.ENGLISH);
            nf.setRoundingMode(BigDecimal.ROUND_DOWN);
            nf.setMinimumFractionDigits(i);
            nf.setMaximumFractionDigits(i);
            MeasureFormat mf = MeasureFormat.getInstance(
                    ULocale.ENGLISH, FormatWidth.WIDE, nf);
            assertEquals("Test10219", expected[j][i], mf.format(new Measure(values[j], MeasureUnit.MINUTE)));
        }
    }
}
 
Example 2
Source File: MeasureUnitTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoubleZero() {
    ULocale en = new ULocale("en");
    NumberFormat nf = NumberFormat.getInstance(en);
    nf.setMinimumFractionDigits(2);
    nf.setMaximumFractionDigits(2);
    MeasureFormat mf = MeasureFormat.getInstance(en, FormatWidth.WIDE, nf);
    assertEquals(
            "Positive Rounding",
            "4 hours, 23 minutes, 16.00 seconds",
            mf.formatMeasures(
                    new Measure(4.7, MeasureUnit.HOUR),
                    new Measure(23, MeasureUnit.MINUTE),
                    new Measure(16, MeasureUnit.SECOND)));
    assertEquals(
            "Negative Rounding",
            "-4 hours, 23 minutes, 16.00 seconds",
            mf.formatMeasures(
                    new Measure(-4.7, MeasureUnit.HOUR),
                    new Measure(23, MeasureUnit.MINUTE),
                    new Measure(16, MeasureUnit.SECOND)));

}
 
Example 3
Source File: BigNumberFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void Test4161100() {
    NumberFormat f = NumberFormat.getInstance();
    f.setMinimumFractionDigits(1);
    f.setMaximumFractionDigits(1);
    double a = -0.09;
    String s = f.format(a);
    logln(a + " x " +
          ((DecimalFormat) f).toPattern() + " = " +
          s);
    if (!s.equals("-0.1")) {
        errln("FAIL");
    }
}
 
Example 4
Source File: PluralRulesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestBasicFraction() {
    String[][] tests = { { "en", "one: j is 1" }, { "1", "0", "1", "one" }, { "1", "2", "1.00", "other" }, };
    ULocale locale = null;
    NumberFormat nf = null;
    PluralRules pr = null;

    for (String[] row : tests) {
        switch (row.length) {
        case 2:
            locale = ULocale.forLanguageTag(row[0]);
            nf = NumberFormat.getInstance(locale);
            pr = PluralRules.createRules(row[1]);
            break;
        case 4:
            double n = Double.parseDouble(row[0]);
            int minFracDigits = Integer.parseInt(row[1]);
            nf.setMinimumFractionDigits(minFracDigits);
            String expectedFormat = row[2];
            String expectedKeyword = row[3];

            UFieldPosition pos = new UFieldPosition();
            String formatted = nf.format(1.0, new StringBuffer(), pos).toString();
            int countVisibleFractionDigits = pos.getCountVisibleFractionDigits();
            long fractionDigits = pos.getFractionDigits();
            String keyword = pr.select(n, countVisibleFractionDigits, fractionDigits);
            assertEquals("Formatted " + n + "\t" + minFracDigits, expectedFormat, formatted);
            assertEquals("Keyword " + n + "\t" + minFracDigits, expectedKeyword, keyword);
            break;
        default:
            throw new RuntimeException();
        }
    }
}
 
Example 5
Source File: NumberFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * alphaWorks upgrade
 */
@Test
public void Test4161100() {
    NumberFormat nf = NumberFormat.getInstance(Locale.US);
    nf.setMinimumFractionDigits(1);
    nf.setMaximumFractionDigits(1);
    double a = -0.09;
    String s = nf.format(a);
    logln(a + " x " +
          ((DecimalFormat) nf).toPattern() + " = " + s);
    if (!s.equals("-0.1")) {
        errln("FAIL");
    }
}
 
Example 6
Source File: NumberRegressionTests.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 4233840: NumberFormat does not round correctly
 */
@Test
public void test4233840() {
    float f = 0.0099f;

    NumberFormat nf = new DecimalFormat("0.##", new DecimalFormatSymbols(Locale.US));
nf.setMinimumFractionDigits(2);

String result = nf.format(f);

if (!result.equals("0.01")) {
    errln("FAIL: input: " + f + ", expected: 0.01, got: " + result);
}
}
 
Example 7
Source File: RelativeDateTimeFormatterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@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);
    }
}