Search in sources :

Example 96 with NumberFormat

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

the class NumberFormatRoundTripTest method TestNumberFormatRoundTrip.

@Test
public void TestNumberFormatRoundTrip() {
    NumberFormat fmt = null;
    logln("Default Locale");
    logln("Default Number format");
    fmt = NumberFormat.getInstance();
    _test(fmt);
    logln("Currency Format");
    fmt = NumberFormat.getCurrencyInstance();
    _test(fmt);
    logln("Percent Format");
    fmt = NumberFormat.getPercentInstance();
    _test(fmt);
    int locCount = 0;
    final Locale[] loc = NumberFormat.getAvailableLocales();
    if (quick) {
        if (locCount > 5)
            locCount = 5;
        logln("Quick mode: only _testing first 5 Locales");
    }
    for (int i = 0; i < locCount; ++i) {
        logln(loc[i].getDisplayName());
        fmt = NumberFormat.getInstance(loc[i]);
        _test(fmt);
        fmt = NumberFormat.getCurrencyInstance(loc[i]);
        _test(fmt);
        fmt = NumberFormat.getPercentInstance(loc[i]);
        _test(fmt);
    }
    logln("Numeric error " + min_numeric_error + " to " + max_numeric_error);
}
Also used : Locale(java.util.Locale) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 97 with NumberFormat

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

the class IntlTestDateFormatAPIC method TestNameHiding.

/**
 * Test hiding of parse() and format() APIs in the Format hierarchy.
 * We test the entire hierarchy, even though this test is located in
 * the DateFormat API test.
 */
@Test
public void TestNameHiding() {
    // N.B.: This test passes if it COMPILES, since it's a test of
    // compile-time name hiding.
    Date dateObj = new Date(0);
    Number numObj = new Double(3.1415926535897932384626433832795);
    StringBuffer strBuffer = new StringBuffer("");
    String str;
    FieldPosition fpos = new FieldPosition(0);
    ParsePosition ppos = new ParsePosition(0);
    // DateFormat calling Format API
    {
        logln("DateFormat");
        DateFormat dateFmt = DateFormat.getInstance();
        if (dateFmt != null) {
            str = dateFmt.format(dateObj);
            strBuffer = dateFmt.format(dateObj, strBuffer, fpos);
        } else {
            errln("FAIL: Can't create DateFormat");
        }
    }
    // SimpleDateFormat calling Format & DateFormat API
    {
        logln("SimpleDateFormat");
        SimpleDateFormat sdf = new SimpleDateFormat();
        // Format API
        str = sdf.format(dateObj);
        strBuffer = sdf.format(dateObj, strBuffer, fpos);
        // DateFormat API
        strBuffer = sdf.format(new Date(0), strBuffer, fpos);
        str = sdf.format(new Date(0));
        try {
            sdf.parse(str);
            sdf.parse(str, ppos);
        } catch (java.text.ParseException pe) {
            System.out.println(pe);
        }
    }
    // NumberFormat calling Format API
    {
        logln("NumberFormat");
        NumberFormat fmt = NumberFormat.getInstance();
        if (fmt != null) {
            str = fmt.format(numObj);
            strBuffer = fmt.format(numObj, strBuffer, fpos);
        } else {
            errln("FAIL: Can't create NumberFormat");
        }
    }
    // DecimalFormat calling Format & NumberFormat API
    {
        logln("DecimalFormat");
        DecimalFormat fmt = new DecimalFormat();
        // Format API
        str = fmt.format(numObj);
        strBuffer = fmt.format(numObj, strBuffer, fpos);
        // NumberFormat API
        str = fmt.format(2.71828);
        str = fmt.format(1234567);
        strBuffer = fmt.format(1.41421, strBuffer, fpos);
        strBuffer = fmt.format(9876543, strBuffer, fpos);
        Number obj = fmt.parse(str, ppos);
        try {
            obj = fmt.parse(str);
            if (obj == null) {
                errln("FAIL: The format object could not parse the string : " + str);
            }
        } catch (java.text.ParseException pe) {
            System.out.println(pe);
        }
    }
// ICU4J have not the classes ChoiceFormat and MessageFormat
/*
        // ChoiceFormat calling Format & NumberFormat API
        {
            logln("ChoiceFormat");
            ChoiceFormat fmt = new ChoiceFormat("0#foo|1#foos|2#foos");
            // Format API
            str = fmt.format(numObj);
            strBuffer = fmt.format(numObj, strBuffer, fpos);
            // NumberFormat API
            str = fmt.format(2.71828);
            str = fmt.format(1234567);
            strBuffer = fmt.format(1.41421, strBuffer, fpos);
            strBuffer = fmt.format(9876543, strBuffer, fpos);
            Number obj = fmt.parse(str, ppos);
            try {
                obj = fmt.parse(str);
            } catch (java.text.ParseException pe) {
                System.out.println(pe);
            }
        }
    
        
        // MessageFormat calling Format API
        {
            logln("MessageFormat");
            MessageFormat fmt = new MessageFormat("");
            // Format API
            // We use dateObj, which MessageFormat should reject.
            // We're testing name hiding, not the format method.
            try {
                str = fmt.format(dateObj);
            } catch (Exception e) {
                //e.printStackTrace();
            }
            try {
                strBuffer = fmt.format(dateObj, strBuffer, fpos);
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
        */
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) FieldPosition(java.text.FieldPosition) Date(java.util.Date) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 98 with NumberFormat

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

the class NumberFormatRegressionTest method TestT9293.

/*
     * Test case for #9293
     * Parsing currency in strict mode
     */
@Test
public void TestT9293() {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    fmt.setParseStrict(true);
    final int val = 123456;
    String txt = fmt.format(123456);
    ParsePosition pos = new ParsePosition(0);
    Number num = fmt.parse(txt, pos);
    if (pos.getErrorIndex() >= 0) {
        errln("FAIL: Parsing " + txt + " - error index: " + pos.getErrorIndex());
    } else if (val != num.intValue()) {
        errln("FAIL: Parsed result: " + num + " - expected: " + val);
    }
}
Also used : NumberFormat(android.icu.text.NumberFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 99 with NumberFormat

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

the class NumberFormatRegressionTest method TestT5698.

/*
     * Test case for ticket#5698 - parsing extremely large/small values
     */
@Test
public void TestT5698() {
    final String[] data = { "12345679E66666666666666666", "-12345679E66666666666666666", // exponent > max int
    ".1E2147483648", // exponent == max int
    ".1E2147483647", // exponent == min int
    ".1E-2147483648", // exponent < min int
    ".1E-2147483649", // value > max double
    "1.23E350", // value < max double
    "1.23E300", // value < min double
    "-1.23E350", // value > min double
    "-1.23E300", // value = smallest non-zero double
    "4.9E-324", // 0 < value < smallest non-zero positive double0
    "1.0E-325", // 0 > value > largest non-zero negative double
    "-1.0E-325" };
    final double[] expected = { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 0.0, 0.0, Double.POSITIVE_INFINITY, 1.23e300d, Double.NEGATIVE_INFINITY, -1.23e300d, 4.9e-324d, 0.0, -0.0 };
    NumberFormat nfmt = NumberFormat.getInstance();
    for (int i = 0; i < data.length; i++) {
        try {
            Number n = nfmt.parse(data[i]);
            if (expected[i] != n.doubleValue()) {
                errln("Failed: Parsed result for " + data[i] + ": " + n.doubleValue() + " / expected: " + expected[i]);
            }
        } catch (ParseException pe) {
            errln("Failed: ParseException is thrown for " + data[i]);
        }
    }
}
Also used : ParseException(java.text.ParseException) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 100 with NumberFormat

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

the class NumberFormatTest method TestRegistration.

@Test
public void TestRegistration() {
    final ULocale SRC_LOC = ULocale.FRANCE;
    final ULocale SWAP_LOC = ULocale.US;
    class TestFactory extends SimpleNumberFormatFactory {

        NumberFormat currencyStyle;

        TestFactory() {
            super(SRC_LOC, true);
            currencyStyle = NumberFormat.getIntegerInstance(SWAP_LOC);
        }

        @Override
        public NumberFormat createFormat(ULocale loc, int formatType) {
            if (formatType == FORMAT_CURRENCY) {
                return currencyStyle;
            }
            return null;
        }
    }
    NumberFormat f0 = NumberFormat.getIntegerInstance(SWAP_LOC);
    NumberFormat f1 = NumberFormat.getIntegerInstance(SRC_LOC);
    NumberFormat f2 = NumberFormat.getCurrencyInstance(SRC_LOC);
    Object key = NumberFormat.registerFactory(new TestFactory());
    NumberFormat f3 = NumberFormat.getCurrencyInstance(SRC_LOC);
    NumberFormat f4 = NumberFormat.getIntegerInstance(SRC_LOC);
    // restore for other tests
    NumberFormat.unregister(key);
    NumberFormat f5 = NumberFormat.getCurrencyInstance(SRC_LOC);
    float n = 1234.567f;
    logln("f0 swap int: " + f0.format(n));
    logln("f1 src int: " + f1.format(n));
    logln("f2 src cur: " + f2.format(n));
    logln("f3 reg cur: " + f3.format(n));
    logln("f4 reg int: " + f4.format(n));
    logln("f5 unreg cur: " + f5.format(n));
    if (!f3.format(n).equals(f0.format(n))) {
        errln("registered service did not match");
    }
    if (!f4.format(n).equals(f1.format(n))) {
        errln("registered service did not inherit");
    }
    if (!f5.format(n).equals(f2.format(n))) {
        errln("unregistered service did not match original");
    }
}
Also used : ULocale(android.icu.util.ULocale) SimpleNumberFormatFactory(android.icu.text.NumberFormat.SimpleNumberFormatFactory) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) 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