Search in sources :

Example 16 with DecimalFormat

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

the class BigNumberFormatTest method TestExponent.

@Test
public void TestExponent() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    DecimalFormat fmt1 = new DecimalFormat("0.###E0", US);
    DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US);
    Number n = new Long(1234);
    expect(fmt1, n, "1.234E3");
    expect(fmt2, n, "1.234E+3");
    expect(fmt1, "1.234E3", n);
    // Either format should parse "E+3"
    expect(fmt1, "1.234E+3", n);
    expect(fmt2, "1.234E+3", n);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 17 with DecimalFormat

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

the class BigNumberFormatTest method TestPad.

/**
 */
@Test
public void TestPad() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    expect(new DecimalFormat("*^##.##", US), new Object[] { new Long(0), "^^^^0", new Double(-1.3), "^-1.3" });
    expect(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US), new Object[] { new Long(0), "0.0E0______ g-m/s^2", new Double(1.0 / 3), "333.333E-3_ g-m/s^2" });
    expect(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US), new Object[] { new Long(0), "0.0______ g-m/s^2", new Double(1.0 / 3), "0.33333__ g-m/s^2" });
    expect(new DecimalFormat("*x#,###,###,##0.00;*x(#,###,###,##0.00)", US), new Object[] { new Long(-100), "xxxxxxxx(100.00)", new Long(-1000), "xxxxxx(1,000.00)", new Long(-1000000), "xx(1,000,000.00)", new Long(-1000000000), "(1,000,000,000.00)" });
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 18 with DecimalFormat

use of android.icu.text.DecimalFormat 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 19 with DecimalFormat

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

the class BigNumberFormatTest method TestSecondaryGrouping.

/**
 * Test the functioning of the secondary grouping value.
 */
@Test
public void TestSecondaryGrouping() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    DecimalFormat f = new DecimalFormat("#,##,###", US);
    expect(f, new Long(123456789), "12,34,56,789");
    expectPat(f, "#,##,###");
    f.applyPattern("#,###");
    f.setSecondaryGroupingSize(4);
    expect(f, new Long(123456789), "12,3456,789");
    expectPat(f, "#,####,###");
    // On Sun JDK 1.2-1.3, the hi_IN locale uses '0' for a zero digit,
    // but on IBM JDK 1.2-1.3, the locale uses U+0966.
    f = (DecimalFormat) NumberFormat.getInstance(new Locale("hi", "IN"));
    String str = transmute("1,87,65,43,210", f.getDecimalFormatSymbols().getZeroDigit());
    expect(f, new Long(1876543210), str);
}
Also used : Locale(java.util.Locale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) DecimalFormat(android.icu.text.DecimalFormat) Test(org.junit.Test)

Example 20 with DecimalFormat

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

the class BigNumberFormatTest method TestScientific.

/**
 */
@Test
public void TestScientific() {
    DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);
    /*For ICU compatibility [Richard/GCL]*/
    expect(NumberFormat.getScientificInstance(Locale.US), new Number[] { new Double(12345.678901), new java.math.BigDecimal("12345.678901") }, "1.2345678901E4");
    expect(new DecimalFormat("##0.###E0", US), new Double(12345), "12.34E3");
    expect(new DecimalFormat("##0.###E0", US), new Double(12345.00001), "12.35E3");
    expect(new DecimalFormat("##0.####E0", US), new Number[] { new Integer(12345), new Long(12345), new java.math.BigDecimal("12345.4999"), new java.math.BigDecimal("12344.5001") }, "12.345E3");
    expect(new DecimalFormat("##0.####E0", US), new Number[] { new java.math.BigDecimal("12345.5000"), new java.math.BigDecimal("12346.5000") }, "12.346E3");
    /*For ICU compatibility [Richard/GCL]*/
    expect(NumberFormat.getScientificInstance(Locale.FRANCE), new Double(12345.678901), "1,2345678901E4");
    expect(new DecimalFormat("##0.####E0", US), new Double(789.12345e-9), "789.12E-9");
    expect(new DecimalFormat("##0.####E0", US), new Double(780.e-9), "780E-9");
    expect(new DecimalFormat(".###E0", US), new Double(45678), ".457E5");
    expect(new DecimalFormat(".###E0", US), new Long(0), ".0E0");
    expect(new DecimalFormat[] { new DecimalFormat("#E0", US), new DecimalFormat("##E0", US), new DecimalFormat("####E0", US), new DecimalFormat("0E0", US), new DecimalFormat("00E0", US), new DecimalFormat("000E0", US) }, new Long(45678000), new String[] { "4.5678E7", "45.678E6", "4567.8E4", "5E7", "46E6", "457E5" });
    expect(new DecimalFormat("###E0", US), new Object[] { new Double(0.0000123), "12.3E-6", new Double(0.000123), "123E-6", // Cafe VM messes up Double(0.00123)
    new java.math.BigDecimal("0.00123"), // Cafe VM messes up Double(0.00123)
    "1.23E-3", new Double(0.0123), "12.3E-3", new Double(0.123), "123E-3", new Double(1.23), "1.23E0", new Double(12.3), "12.3E0", new Double(123), "123E0", new Double(1230), "1.23E3" });
    expect(new DecimalFormat("0.#E+00", US), new Object[] { new Double(0.00012), "1.2E-04", new Long(12000), "1.2E+04" });
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) 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