Search in sources :

Example 76 with DecimalFormat

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

the class NumberFormatTest method expect.

// Format test
public void expect(NumberFormat fmt, Number n, String exp, boolean rt) {
    StringBuffer saw = new StringBuffer();
    FieldPosition pos = new FieldPosition(0);
    fmt.format(n, saw, pos);
    String pat = ((DecimalFormat) fmt).toPattern();
    if (saw.toString().equals(exp)) {
        logln("Ok   " + n + " x " + pat + " = \"" + saw + "\"");
        // => string => number2, might have number2 != number):
        if (rt) {
            try {
                Number n2 = fmt.parse(exp);
                StringBuffer saw2 = new StringBuffer();
                fmt.format(n2, saw2, pos);
                if (!saw2.toString().equals(exp)) {
                    errln("expect() format test rt, locale " + fmt.getLocale(ULocale.VALID_LOCALE) + ", FAIL \"" + exp + "\" => " + n2 + " => \"" + saw2 + '"');
                }
            } catch (ParseException e) {
                errln("expect() format test rt, locale " + fmt.getLocale(ULocale.VALID_LOCALE) + ", " + e.getMessage());
                return;
            }
        }
    } else {
        errln("expect() format test, locale " + fmt.getLocale(ULocale.VALID_LOCALE) + ", FAIL " + n + " x " + pat + " = \"" + saw + "\", expected \"" + exp + "\"");
    }
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ParseException(java.text.ParseException) FieldPosition(java.text.FieldPosition)

Example 77 with DecimalFormat

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

the class NumberFormatTest method TestStringSymbols.

@Test
public void TestStringSymbols() {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(ULocale.US);
    String[] customDigits = { "(0)", "(1)", "(2)", "(3)", "(4)", "(5)", "(6)", "(7)", "(8)", "(9)" };
    symbols.setDigitStrings(customDigits);
    symbols.setDecimalSeparatorString("~~");
    symbols.setGroupingSeparatorString("^^");
    DecimalFormat fmt = new DecimalFormat("#,##0.0#", symbols);
    expect2(fmt, 1234567.89, "(1)^^(2)(3)(4)^^(5)(6)(7)~~(8)(9)");
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 78 with DecimalFormat

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

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 79 with DecimalFormat

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

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 80 with DecimalFormat

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

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)

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