Search in sources :

Example 81 with DecimalFormat

use of android.icu.text.DecimalFormat in project android_packages_apps_Settings by SudaMod.

the class FileSizeFormatter method getNumberFormatter.

private static NumberFormat getNumberFormatter(Locale locale, int fractionDigits) {
    final NumberFormat numberFormatter = NumberFormat.getInstance(locale);
    numberFormatter.setMinimumFractionDigits(fractionDigits);
    numberFormatter.setMaximumFractionDigits(fractionDigits);
    numberFormatter.setGroupingUsed(false);
    if (numberFormatter instanceof DecimalFormat) {
        // We do this only for DecimalFormat, since in the general NumberFormat case, calling
        // setRoundingMode may throw an exception.
        numberFormatter.setRoundingMode(BigDecimal.ROUND_HALF_UP);
    }
    return numberFormatter;
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) NumberFormat(android.icu.text.NumberFormat)

Example 82 with DecimalFormat

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

the class NumberFormatTest method TestCurrencyPlurals.

@Test
public void TestCurrencyPlurals() {
    String[][] tests = { { "en", "USD", "1", "1 US dollar" }, { "en", "USD", "1.0", "1.0 US dollars" }, { "en", "USD", "1.00", "1.00 US dollars" }, { "en", "USD", "1.99", "1.99 US dollars" }, { "en", "AUD", "1", "1 Australian dollar" }, { "en", "AUD", "1.00", "1.00 Australian dollars" }, { "sl", "USD", "1", "1 ameri\u0161ki dolar" }, { "sl", "USD", "2", "2 ameri\u0161ka dolarja" }, { "sl", "USD", "3", "3 ameri\u0161ki dolarji" }, { "sl", "USD", "5", "5 ameriških dolarjev" }, { "fr", "USD", "1.99", "1,99 dollar des États-Unis" }, { "ru", "RUB", "1", "1 \u0440\u043E\u0441\u0441\u0438\u0439\u0441\u043A\u0438\u0439 \u0440\u0443\u0431\u043B\u044C" }, { "ru", "RUB", "2", "2 \u0440\u043E\u0441\u0441\u0438\u0439\u0441\u043A\u0438\u0445 \u0440\u0443\u0431\u043B\u044F" }, { "ru", "RUB", "5", "5 \u0440\u043E\u0441\u0441\u0438\u0439\u0441\u043A\u0438\u0445 \u0440\u0443\u0431\u043B\u0435\u0439" } };
    for (String[] test : tests) {
        DecimalFormat numberFormat = (DecimalFormat) DecimalFormat.getInstance(new ULocale(test[0]), NumberFormat.PLURALCURRENCYSTYLE);
        numberFormat.setCurrency(Currency.getInstance(test[1]));
        double number = Double.parseDouble(test[2]);
        int dotPos = test[2].indexOf('.');
        int decimals = dotPos < 0 ? 0 : test[2].length() - dotPos - 1;
        int digits = dotPos < 0 ? test[2].length() : test[2].length() - 1;
        numberFormat.setMaximumFractionDigits(decimals);
        numberFormat.setMinimumFractionDigits(decimals);
        String actual = numberFormat.format(number);
        assertEquals(test[0] + "\t" + test[1] + "\t" + test[2], test[3], actual);
        numberFormat.setMaximumSignificantDigits(digits);
        numberFormat.setMinimumSignificantDigits(digits);
        actual = numberFormat.format(number);
        assertEquals(test[0] + "\t" + test[1] + "\t" + test[2], test[3], actual);
    }
}
Also used : ULocale(android.icu.util.ULocale) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 83 with DecimalFormat

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

the class NumberFormatTest method TestNonpositiveMultiplier.

@Test
public void TestNonpositiveMultiplier() {
    DecimalFormat df = new DecimalFormat("0");
    try {
        df.setMultiplier(0);
        // bad
        errln("DecimalFormat.setMultiplier(0) did not throw an IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
    // good
    }
    try {
        df.setMultiplier(-1);
        if (df.getMultiplier() != -1) {
            errln("DecimalFormat.setMultiplier(-1) did not change the multiplier to -1");
            return;
        }
    // good
    } catch (IllegalArgumentException ex) {
        // bad
        errln("DecimalFormat.setMultiplier(-1) threw an IllegalArgumentException");
        return;
    }
    expect(df, "1122.123", -1122.123);
    expect(df, "-1122.123", 1122.123);
    expect(df, "1.2", -1.2);
    expect(df, "-1.2", 1.2);
    expect2(df, Long.MAX_VALUE, BigInteger.valueOf(Long.MAX_VALUE).negate().toString());
    expect2(df, Long.MIN_VALUE, BigInteger.valueOf(Long.MIN_VALUE).negate().toString());
    expect2(df, Long.MAX_VALUE / 2, BigInteger.valueOf(Long.MAX_VALUE / 2).negate().toString());
    expect2(df, Long.MIN_VALUE / 2, BigInteger.valueOf(Long.MIN_VALUE / 2).negate().toString());
    expect2(df, BigDecimal.valueOf(Long.MAX_VALUE), BigDecimal.valueOf(Long.MAX_VALUE).negate().toString());
    expect2(df, BigDecimal.valueOf(Long.MIN_VALUE), BigDecimal.valueOf(Long.MIN_VALUE).negate().toString());
    expect2(df, java.math.BigDecimal.valueOf(Long.MAX_VALUE), java.math.BigDecimal.valueOf(Long.MAX_VALUE).negate().toString());
    expect2(df, java.math.BigDecimal.valueOf(Long.MIN_VALUE), java.math.BigDecimal.valueOf(Long.MIN_VALUE).negate().toString());
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 84 with DecimalFormat

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

the class NumberFormatTest method TestPerMill.

@Test
public void TestPerMill() {
    DecimalFormat fmt = new DecimalFormat("###.###\u2030");
    assertEquals("0.4857 x ###.###\u2030", "485.7\u2030", fmt.format(0.4857));
    DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.ENGLISH);
    sym.setPerMill('m');
    DecimalFormat fmt2 = new DecimalFormat("", sym);
    fmt2.applyLocalizedPattern("###.###m");
    assertEquals("0.4857 x ###.###m", "485.7m", fmt2.format(0.4857));
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 85 with DecimalFormat

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

the class NumberFormatTest method TestLenientSymbolParsing.

/*
     * Testing lenient decimal/grouping separator parsing
     */
@Test
public void TestLenientSymbolParsing() {
    DecimalFormat fmt = new DecimalFormat();
    DecimalFormatSymbols sym = new DecimalFormatSymbols();
    expect(fmt, "12\u300234", 12.34);
    // Ticket#7345 - case 1
    // Even strict parsing, the decimal separator set in the symbols
    // should be successfully parsed.
    sym.setDecimalSeparator('\u3002');
    // non-strict
    fmt.setDecimalFormatSymbols(sym);
    // strict - failed before the fix for #7345
    fmt.setParseStrict(true);
    expect(fmt, "23\u300245", 23.45);
    fmt.setParseStrict(false);
    // Ticket#7345 - case 2
    // Decimal separator variants other than DecimalFormatSymbols.decimalSeparator
    // should not hide the grouping separator DecimalFormatSymbols.groupingSeparator.
    sym.setDecimalSeparator('.');
    sym.setGroupingSeparator(',');
    fmt.setDecimalFormatSymbols(sym);
    expect(fmt, "1,234.56", 1234.56);
    sym.setGroupingSeparator('\uFF61');
    fmt.setDecimalFormatSymbols(sym);
    expect(fmt, "2\uFF61345.67", 2345.67);
    // Ticket#7128
    // 
    sym.setGroupingSeparator(',');
    fmt.setDecimalFormatSymbols(sym);
    String skipExtSepParse = ICUConfig.get("android.icu.text.DecimalFormat.SkipExtendedSeparatorParsing", "false");
    if (skipExtSepParse.equals("true")) {
        // When the property SkipExtendedSeparatorParsing is true,
        // DecimalFormat does not use the extended equivalent separator
        // data and only uses the one in DecimalFormatSymbols.
        expect(fmt, "23 456", 23);
    } else {
        // Lenient separator parsing is enabled by default.
        // A space character below is interpreted as a
        // group separator, even ',' is used as grouping
        // separator in the symbols.
        expect(fmt, "12 345", 12345);
    }
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Aggregations

DecimalFormat (android.icu.text.DecimalFormat)150 Test (org.junit.Test)138 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)70 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)57 NumberFormat (android.icu.text.NumberFormat)30 ULocale (android.icu.util.ULocale)28 FieldPosition (java.text.FieldPosition)25 ParseException (java.text.ParseException)23 Locale (java.util.Locale)18 ParsePosition (java.text.ParsePosition)15 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)12 BigDecimal (android.icu.math.BigDecimal)10 IOException (java.io.IOException)8 InvalidObjectException (java.io.InvalidObjectException)5 BigInteger (java.math.BigInteger)5 ScientificNumberFormatter (android.icu.text.ScientificNumberFormatter)4 SimpleDateFormat (android.icu.text.SimpleDateFormat)4 Format (java.text.Format)4 DateFormat (android.icu.text.DateFormat)3 MessageFormat (android.icu.text.MessageFormat)3