Search in sources :

Example 51 with NumberFormat

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

the class NumberFormatRegressionTest method Test4408066.

/**
 * Test getIntegerInstance();
 */
@Test
public void Test4408066() {
    NumberFormat nf1 = NumberFormat.getIntegerInstance();
    NumberFormat nf2 = NumberFormat.getIntegerInstance(Locale.CHINA);
    // test isParseIntegerOnly
    if (!nf1.isParseIntegerOnly() || !nf2.isParseIntegerOnly()) {
        errln("Failed : Integer Number Format Instance should set setParseIntegerOnly(true)");
    }
    // Test format
    {
        double[] data = { -3.75, -2.5, -1.5, -1.25, 0, 1.0, 1.25, 1.5, 2.5, 3.75, 10.0, 255.5 };
        String[] expected = { "-4", "-2", "-2", "-1", "0", "1", "1", "2", "2", "4", "10", "256" };
        for (int i = 0; i < data.length; ++i) {
            String result = nf1.format(data[i]);
            if (!result.equals(expected[i])) {
                errln("Failed => Source: " + Double.toString(data[i]) + ";Formatted : " + result + ";but expectted: " + expected[i]);
            }
        }
    }
    // Test parse, Parsing should stop at "."
    {
        String[] data = { "-3.75", "-2.5", "-1.5", "-1.25", "0", "1.0", "1.25", "1.5", "2.5", "3.75", "10.0", "255.5" };
        long[] expected = { -3, -2, -1, -1, 0, 1, 1, 1, 2, 3, 10, 255 };
        for (int i = 0; i < data.length; ++i) {
            Number n = null;
            try {
                n = nf1.parse(data[i]);
            } catch (ParseException e) {
                errln("Failed: " + e.getMessage());
            }
            if (!(n instanceof Long) || (n instanceof Integer)) {
                errln("Failed: Integer Number Format should parse string to Long/Integer");
            }
            if (n.longValue() != expected[i]) {
                errln("Failed=> Source: " + data[i] + ";result : " + n.toString() + ";expected :" + Long.toString(expected[i]));
            }
        }
    }
}
Also used : ParseException(java.text.ParseException) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 52 with NumberFormat

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

the class NumberFormatRegressionTest method TestSurrogatesParsing.

@Test
public void TestSurrogatesParsing() {
    // Test parsing of numbers that use digits from the supplemental planes.
    final String[] data = { // 
    "1\ud801\udca2,3\ud801\udca45.67", // 
    "\ud801\udca1\ud801\udca2,\ud801\udca3\ud801\udca4\ud801\udca5.\ud801\udca6\ud801\udca7\ud801\udca8", "\ud835\udfd2.\ud835\udfd7E-\ud835\udfd1", "\ud835\udfd3.8E-0\ud835\udfd0" };
    final double[] expected = { 12345.67, 12345.678, 0.0049, 0.058 };
    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 53 with NumberFormat

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

the class NumberFormatTest method TestCompatibleCurrencies.

@Test
public void TestCompatibleCurrencies() {
    NumberFormat fmt = NumberFormat.getCurrencyInstance(Locale.US);
    // Yen half-width
    expectParseCurrency(fmt, Currency.getInstance(Locale.JAPAN), "\u00A51,235");
    // Yen full-wdith
    expectParseCurrency(fmt, Currency.getInstance(Locale.JAPAN), "\uFFE51,235");
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 54 with NumberFormat

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

the class NumberFormatTest method TestCurrencyPatterns.

@Test
public void TestCurrencyPatterns() {
    int i;
    Locale[] locs = NumberFormat.getAvailableLocales();
    for (i = 0; i < locs.length; ++i) {
        NumberFormat nf = NumberFormat.getCurrencyInstance(locs[i]);
        // Make sure currency formats do not have a variable number
        // of fraction digits
        int min = nf.getMinimumFractionDigits();
        int max = nf.getMaximumFractionDigits();
        if (min != max) {
            String a = nf.format(1.0);
            String b = nf.format(1.125);
            errln("FAIL: " + locs[i] + " min fraction digits != max fraction digits; " + "x 1.0 => " + a + "; x 1.125 => " + b);
        }
        // Make sure EURO currency formats have exactly 2 fraction digits
        if (nf instanceof DecimalFormat) {
            Currency curr = ((DecimalFormat) nf).getCurrency();
            if (curr != null && "EUR".equals(curr.getCurrencyCode())) {
                if (min != 2 || max != 2) {
                    String a = nf.format(1.0);
                    errln("FAIL: " + locs[i] + " is a EURO format but it does not have 2 fraction digits; " + "x 1.0 => " + a);
                }
            }
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Currency(android.icu.util.Currency) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 55 with NumberFormat

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

the class NumberFormatTest method TestExplicitParents.

@Test
public void TestExplicitParents() {
    // We use these for testing because decimal and grouping separators will be inherited from es_419
    // starting with CLDR 2.0
    String[] DATA = { "es", "CO", "", "1.250,75", "es", "ES", "", "1.250,75", "es", "GQ", "", "1.250,75", "es", "MX", "", "1,250.75", "es", "US", "", "1,250.75", "es", "VE", "", "1.250,75" };
    for (int i = 0; i < DATA.length; i += 4) {
        Locale locale = new Locale(DATA[i], DATA[i + 1], DATA[i + 2]);
        NumberFormat fmt = NumberFormat.getInstance(locale);
        String s = fmt.format(1250.75);
        if (s.equals(DATA[i + 3])) {
            logln("Ok: 1250.75 x " + locale + " => " + s);
        } else {
            errln("FAIL: 1250.75 x " + locale + " => " + s + ", expected " + DATA[i + 3]);
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) 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