Search in sources :

Example 6 with NumberFormat

use of android.icu.text.NumberFormat in project platform_packages_apps_Settings by BlissRoms.

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 7 with NumberFormat

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

the class BigNumberFormatTest method expect.

private void expect(Object fmto, Object numo, Object expo) {
    NumberFormat fmt = null, fmts[] = null;
    Number num = null, nums[] = null;
    String exp = null, exps[] = null;
    if (fmto instanceof NumberFormat[]) {
        fmts = (NumberFormat[]) fmto;
    } else {
        fmt = (NumberFormat) fmto;
    }
    if (numo instanceof Number[]) {
        nums = (Number[]) numo;
    } else {
        num = (Number) numo;
    }
    if (expo instanceof String[]) {
        exps = (String[]) expo;
    } else {
        exp = (String) expo;
    }
    int n = 1;
    if (fmts != null) {
        n = Math.max(n, fmts.length);
    }
    if (nums != null) {
        n = Math.max(n, nums.length);
    }
    if (exps != null) {
        n = Math.max(n, exps.length);
    }
    for (int i = 0; i < n; ++i) {
        expect(fmts == null ? fmt : fmts[i], nums == null ? num : nums[i], exps == null ? exp : exps[i]);
    }
}
Also used : NumberFormat(android.icu.text.NumberFormat)

Example 8 with NumberFormat

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

the class BigNumberFormatTest method Test4161100.

@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");
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 9 with NumberFormat

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

the class BigNumberFormatTest method TestBigDecimalJ28.

@Test
public void TestBigDecimalJ28() {
    String[] DATA = { "1", "1E0", "-1", "-1E0", "0", "0E0", "12e34", "1.2E35", "-12.3e-45", "-1.23E-44", "0.73e-7", "7.3E-8" };
    NumberFormat fmt = NumberFormat.getScientificInstance(Locale.US);
    logln("Pattern: " + ((DecimalFormat) fmt).toPattern());
    for (int i = 0; i < DATA.length; i += 2) {
        String input = DATA[i];
        String exp = DATA[i + 1];
        android.icu.math.BigDecimal bd = new android.icu.math.BigDecimal(input);
        String output = fmt.format(bd);
        if (output.equals(exp)) {
            logln("input=" + input + " num=" + bd + " output=" + output);
        } else {
            errln("FAIL: input=" + input + " num=" + bd + " output=" + output + " expected=" + exp);
        }
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 10 with NumberFormat

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

the class CompactDecimalFormatTest method TestArabicLongStyle.

@Test
public void TestArabicLongStyle() {
    NumberFormat cdf = CompactDecimalFormat.getInstance(new Locale("ar"), CompactStyle.LONG);
    assertEquals("Arabic Long", "\u061C-\u0665\u066B\u0663 \u0623\u0644\u0641", cdf.format(-5300));
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) 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