Search in sources :

Example 21 with NumberFormat

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

the class NumberFormatTest method TestParseSignsAndMarks.

@Test
public void TestParseSignsAndMarks() {
    class SignsAndMarksItem {

        public String locale;

        public boolean lenient;

        public String numString;

        public double value;

        // Simple constructor
        public SignsAndMarksItem(String loc, boolean lnt, String numStr, double val) {
            locale = loc;
            lenient = lnt;
            numString = numStr;
            value = val;
        }
    }
    ;
    final SignsAndMarksItem[] items = { // locale                lenient numString                               value
    new SignsAndMarksItem("en", false, "12", 12), new SignsAndMarksItem("en", true, "12", 12), new SignsAndMarksItem("en", false, "-23", -23), new SignsAndMarksItem("en", true, "-23", -23), // ***
    new SignsAndMarksItem("en", true, "- 23", -23), new SignsAndMarksItem("en", false, "\u200E-23", -23), new SignsAndMarksItem("en", true, "\u200E-23", -23), // ***
    new SignsAndMarksItem("en", true, "\u200E- 23", -23), new SignsAndMarksItem("en@numbers=arab", false, "\u0663\u0664", 34), new SignsAndMarksItem("en@numbers=arab", true, "\u0663\u0664", 34), new SignsAndMarksItem("en@numbers=arab", false, "-\u0664\u0665", -45), new SignsAndMarksItem("en@numbers=arab", true, "-\u0664\u0665", -45), // ***
    new SignsAndMarksItem("en@numbers=arab", true, "- \u0664\u0665", -45), new SignsAndMarksItem("en@numbers=arab", false, "\u200F-\u0664\u0665", -45), new SignsAndMarksItem("en@numbers=arab", true, "\u200F-\u0664\u0665", -45), // ***
    new SignsAndMarksItem("en@numbers=arab", true, "\u200F- \u0664\u0665", -45), new SignsAndMarksItem("en@numbers=arabext", false, "\u06F5\u06F6", 56), new SignsAndMarksItem("en@numbers=arabext", true, "\u06F5\u06F6", 56), new SignsAndMarksItem("en@numbers=arabext", false, "-\u06F6\u06F7", -67), new SignsAndMarksItem("en@numbers=arabext", true, "-\u06F6\u06F7", -67), // ***
    new SignsAndMarksItem("en@numbers=arabext", true, "- \u06F6\u06F7", -67), new SignsAndMarksItem("en@numbers=arabext", false, "\u200E-\u200E\u06F6\u06F7", -67), new SignsAndMarksItem("en@numbers=arabext", true, "\u200E-\u200E\u06F6\u06F7", -67), // ***
    new SignsAndMarksItem("en@numbers=arabext", true, "\u200E-\u200E \u06F6\u06F7", -67), new SignsAndMarksItem("he", false, "12", 12), new SignsAndMarksItem("he", true, "12", 12), new SignsAndMarksItem("he", false, "-23", -23), new SignsAndMarksItem("he", true, "-23", -23), // ***
    new SignsAndMarksItem("he", true, "- 23", -23), new SignsAndMarksItem("he", false, "\u200E-23", -23), new SignsAndMarksItem("he", true, "\u200E-23", -23), // ***
    new SignsAndMarksItem("he", true, "\u200E- 23", -23), new SignsAndMarksItem("ar", false, "\u0663\u0664", 34), new SignsAndMarksItem("ar", true, "\u0663\u0664", 34), new SignsAndMarksItem("ar", false, "-\u0664\u0665", -45), new SignsAndMarksItem("ar", true, "-\u0664\u0665", -45), // ***
    new SignsAndMarksItem("ar", true, "- \u0664\u0665", -45), new SignsAndMarksItem("ar", false, "\u200F-\u0664\u0665", -45), new SignsAndMarksItem("ar", true, "\u200F-\u0664\u0665", -45), // ***
    new SignsAndMarksItem("ar", true, "\u200F- \u0664\u0665", -45), new SignsAndMarksItem("ar_MA", false, "12", 12), new SignsAndMarksItem("ar_MA", true, "12", 12), new SignsAndMarksItem("ar_MA", false, "-23", -23), new SignsAndMarksItem("ar_MA", true, "-23", -23), // ***
    new SignsAndMarksItem("ar_MA", true, "- 23", -23), new SignsAndMarksItem("ar_MA", false, "\u200E-23", -23), new SignsAndMarksItem("ar_MA", true, "\u200E-23", -23), // ***
    new SignsAndMarksItem("ar_MA", true, "\u200E- 23", -23), new SignsAndMarksItem("fa", false, "\u06F5\u06F6", 56), new SignsAndMarksItem("fa", true, "\u06F5\u06F6", 56), new SignsAndMarksItem("fa", false, "\u2212\u06F6\u06F7", -67), new SignsAndMarksItem("fa", true, "\u2212\u06F6\u06F7", -67), // ***
    new SignsAndMarksItem("fa", true, "\u2212 \u06F6\u06F7", -67), new SignsAndMarksItem("fa", false, "\u200E\u2212\u200E\u06F6\u06F7", -67), new SignsAndMarksItem("fa", true, "\u200E\u2212\u200E\u06F6\u06F7", -67), // ***
    new SignsAndMarksItem("fa", true, "\u200E\u2212\u200E \u06F6\u06F7", -67), new SignsAndMarksItem("ps", false, "\u06F5\u06F6", 56), new SignsAndMarksItem("ps", true, "\u06F5\u06F6", 56), new SignsAndMarksItem("ps", false, "-\u06F6\u06F7", -67), new SignsAndMarksItem("ps", true, "-\u06F6\u06F7", -67), // ***
    new SignsAndMarksItem("ps", true, "- \u06F6\u06F7", -67), new SignsAndMarksItem("ps", false, "\u200E-\u200E\u06F6\u06F7", -67), new SignsAndMarksItem("ps", true, "\u200E-\u200E\u06F6\u06F7", -67), // ***
    new SignsAndMarksItem("ps", true, "\u200E-\u200E \u06F6\u06F7", -67), new SignsAndMarksItem("ps", false, "-\u200E\u06F6\u06F7", -67), new SignsAndMarksItem("ps", true, "-\u200E\u06F6\u06F7", -67), // ***
    new SignsAndMarksItem("ps", true, "-\u200E \u06F6\u06F7", -67) };
    for (SignsAndMarksItem item : items) {
        ULocale locale = new ULocale(item.locale);
        NumberFormat numfmt = NumberFormat.getInstance(locale);
        if (numfmt != null) {
            numfmt.setParseStrict(!item.lenient);
            ParsePosition ppos = new ParsePosition(0);
            Number num = numfmt.parse(item.numString, ppos);
            if (num != null && ppos.getIndex() == item.numString.length()) {
                double parsedValue = num.doubleValue();
                if (parsedValue != item.value) {
                    errln("FAIL: locale " + item.locale + ", lenient " + item.lenient + ", parse of \"" + item.numString + "\" gives value " + parsedValue);
                }
            } else {
                errln("FAIL: locale " + item.locale + ", lenient " + item.lenient + ", parse of \"" + item.numString + "\" gives position " + ppos.getIndex());
            }
        } else {
            errln("FAIL: NumberFormat.getInstance for locale " + item.locale);
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 22 with NumberFormat

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

the class NumberFormatTest method TestSpaceParsing.

@Test
public void TestSpaceParsing() {
    // the data are:
    // the string to be parsed, parsed position, parsed error index
    String[][] DATA = { { "$124", "4", "-1" }, { "$124 $124", "4", "-1" }, { "$124 ", "4", "-1" }, { "$ 124 ", "5", "-1" }, { "$\u00A0124 ", "5", "-1" }, // TODO: need to handle space correctly
    { " $ 124 ", "0", "0" }, // TODO: need to handle space correctly
    { "124$", "0", "3" }, // {"124 $", "5", "-1"}, TODO: OK or NOT?
    { "124 $", "0", "3" } };
    NumberFormat foo = NumberFormat.getCurrencyInstance();
    for (int i = 0; i < DATA.length; ++i) {
        ParsePosition parsePosition = new ParsePosition(0);
        String stringToBeParsed = DATA[i][0];
        int parsedPosition = Integer.parseInt(DATA[i][1]);
        int errorIndex = Integer.parseInt(DATA[i][2]);
        try {
            Number result = foo.parse(stringToBeParsed, parsePosition);
            if (parsePosition.getIndex() != parsedPosition || parsePosition.getErrorIndex() != errorIndex) {
                errln("FAILED parse " + stringToBeParsed + "; parse position: " + parsePosition.getIndex() + "; error position: " + parsePosition.getErrorIndex());
            }
            if (parsePosition.getErrorIndex() == -1 && result.doubleValue() != 124) {
                errln("FAILED parse " + stringToBeParsed + "; value " + result.doubleValue());
            }
        } catch (Exception e) {
            errln("FAILED " + e.toString());
        }
    }
}
Also used : ParseException(java.text.ParseException) IOException(java.io.IOException) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 23 with NumberFormat

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

the class NumberFormatTest method TestContext.

@Test
public void TestContext() {
    // just a minimal sanity check for now
    NumberFormat nfmt = NumberFormat.getInstance();
    DisplayContext context = nfmt.getContext(DisplayContext.Type.CAPITALIZATION);
    if (context != DisplayContext.CAPITALIZATION_NONE) {
        errln("FAIL: Initial NumberFormat.getContext() is not CAPITALIZATION_NONE");
    }
    nfmt.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE);
    context = nfmt.getContext(DisplayContext.Type.CAPITALIZATION);
    if (context != DisplayContext.CAPITALIZATION_FOR_STANDALONE) {
        errln("FAIL: NumberFormat.getContext() does not return the value set, CAPITALIZATION_FOR_STANDALONE");
    }
}
Also used : DisplayContext(android.icu.text.DisplayContext) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 24 with NumberFormat

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

the class NumberFormatTest method Test10419RoundingWith0FractionDigits.

@Test
public void Test10419RoundingWith0FractionDigits() {
    Object[][] data = new Object[][] { { BigDecimal.ROUND_CEILING, 1.488, "2" }, { BigDecimal.ROUND_DOWN, 1.588, "1" }, { BigDecimal.ROUND_FLOOR, 1.588, "1" }, { BigDecimal.ROUND_HALF_DOWN, 1.5, "1" }, { BigDecimal.ROUND_HALF_EVEN, 2.5, "2" }, { BigDecimal.ROUND_HALF_UP, 2.5, "3" }, { BigDecimal.ROUND_UP, 1.5, "2" } };
    NumberFormat nff = NumberFormat.getNumberInstance(ULocale.ENGLISH);
    nff.setMaximumFractionDigits(0);
    for (Object[] item : data) {
        nff.setRoundingMode(((Integer) item[0]).intValue());
        assertEquals("Test10419", item[2], nff.format(item[1]));
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 25 with NumberFormat

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

the class NumberFormatTest method TestNumberingSystems.

/**
 * Test alternate numbering systems
 */
@Test
public void TestNumberingSystems() {
    class TestNumberingSystemItem {

        private final String localeName;

        private final double value;

        private final boolean isRBNF;

        private final String expectedResult;

        TestNumberingSystemItem(String loc, double val, boolean rbnf, String exp) {
            localeName = loc;
            value = val;
            isRBNF = rbnf;
            expectedResult = exp;
        }
    }
    final TestNumberingSystemItem[] DATA = { new TestNumberingSystemItem("en_US@numbers=thai", 1234.567, false, "\u0e51,\u0e52\u0e53\u0e54.\u0e55\u0e56\u0e57"), new TestNumberingSystemItem("en_US@numbers=thai", 1234.567, false, "\u0E51,\u0E52\u0E53\u0E54.\u0E55\u0E56\u0E57"), new TestNumberingSystemItem("en_US@numbers=hebr", 5678.0, true, "\u05D4\u05F3\u05EA\u05E8\u05E2\u05F4\u05D7"), new TestNumberingSystemItem("en_US@numbers=arabext", 1234.567, false, "\u06F1\u066c\u06F2\u06F3\u06F4\u066b\u06F5\u06F6\u06F7"), new TestNumberingSystemItem("de_DE@numbers=foobar", 1234.567, false, "1.234,567"), new TestNumberingSystemItem("ar_EG", 1234.567, false, "\u0661\u066c\u0662\u0663\u0664\u066b\u0665\u0666\u0667"), // fall back to native per TR35
    new TestNumberingSystemItem("th_TH@numbers=traditional", 1234.567, false, "\u0E51,\u0E52\u0E53\u0E54.\u0E55\u0E56\u0E57"), new TestNumberingSystemItem("ar_MA", 1234.567, false, "1.234,567"), new TestNumberingSystemItem("en_US@numbers=hanidec", 1234.567, false, "\u4e00,\u4e8c\u4e09\u56db.\u4e94\u516d\u4e03"), new TestNumberingSystemItem("ta_IN@numbers=native", 1234.567, false, "\u0BE7,\u0BE8\u0BE9\u0BEA.\u0BEB\u0BEC\u0BED"), new TestNumberingSystemItem("ta_IN@numbers=traditional", 1235.0, true, "\u0BF2\u0BE8\u0BF1\u0BE9\u0BF0\u0BEB"), // fall back to default per TR35
    new TestNumberingSystemItem("ta_IN@numbers=finance", 1234.567, false, "1,234.567"), new TestNumberingSystemItem("zh_TW@numbers=native", 1234.567, false, "\u4e00,\u4e8c\u4e09\u56db.\u4e94\u516d\u4e03"), new TestNumberingSystemItem("zh_TW@numbers=traditional", 1234.567, true, "\u4E00\u5343\u4E8C\u767E\u4E09\u5341\u56DB\u9EDE\u4E94\u516D\u4E03"), new TestNumberingSystemItem("zh_TW@numbers=finance", 1234.567, true, "\u58F9\u4EDF\u8CB3\u4F70\u53C3\u62FE\u8086\u9EDE\u4F0D\u9678\u67D2") };
    for (TestNumberingSystemItem item : DATA) {
        ULocale loc = new ULocale(item.localeName);
        NumberFormat fmt = NumberFormat.getInstance(loc);
        if (item.isRBNF) {
            expect3(fmt, item.value, item.expectedResult);
        } else {
            expect2(fmt, item.value, item.expectedResult);
        }
    }
}
Also used : 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