Search in sources :

Example 66 with NumberFormat

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

the class NumberFormatTest method TestMiscCurrencyParsing.

@Test
public void TestMiscCurrencyParsing() {
    String[][] DATA = { // each has: string to be parsed, parsed position, error position
    { "1.00 ", "0", "4" }, { "1.00 UAE dirha", "0", "4" }, { "1.00 us dollar", "14", "-1" }, { "1.00 US DOLLAR", "14", "-1" }, { "1.00 usd", "0", "4" } };
    ULocale locale = new ULocale("en_US");
    for (int i = 0; i < DATA.length; ++i) {
        String stringToBeParsed = DATA[i][0];
        int parsedPosition = Integer.parseInt(DATA[i][1]);
        int errorIndex = Integer.parseInt(DATA[i][2]);
        NumberFormat numFmt = NumberFormat.getInstance(locale, NumberFormat.CURRENCYSTYLE);
        ParsePosition parsePosition = new ParsePosition(0);
        Number val = numFmt.parse(stringToBeParsed, parsePosition);
        if (parsePosition.getIndex() != parsedPosition || parsePosition.getErrorIndex() != errorIndex) {
            errln("FAIL: parse failed. expected error position: " + errorIndex + "; actual: " + parsePosition.getErrorIndex());
            errln("FAIL: parse failed. expected position: " + parsedPosition + "; actual: " + parsePosition.getIndex());
        }
        if (parsePosition.getErrorIndex() == -1 && val.doubleValue() != 1.00) {
            errln("FAIL: parse failed. expected 1.00, actual:" + val);
        }
    }
}
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 67 with NumberFormat

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

the class NumberFormatTest method TestMultiCurrencySign.

@Test
public void TestMultiCurrencySign() {
    String[][] DATA = { // for US locale
    { "en_US", "\u00A4#,##0.00;-\u00A4#,##0.00", "1234.56", "$1,234.56", "USD1,234.56", "US dollars1,234.56" }, { "en_US", "\u00A4#,##0.00;-\u00A4#,##0.00", "-1234.56", "-$1,234.56", "-USD1,234.56", "-US dollars1,234.56" }, { "en_US", "\u00A4#,##0.00;-\u00A4#,##0.00", "1", "$1.00", "USD1.00", "US dollars1.00" }, // for CHINA locale
    { "zh_CN", "\u00A4#,##0.00;(\u00A4#,##0.00)", "1234.56", "\uFFE51,234.56", "CNY1,234.56", "\u4EBA\u6C11\u5E011,234.56" }, { "zh_CN", "\u00A4#,##0.00;(\u00A4#,##0.00)", "-1234.56", "(\uFFE51,234.56)", "(CNY1,234.56)", "(\u4EBA\u6C11\u5E011,234.56)" }, { "zh_CN", "\u00A4#,##0.00;(\u00A4#,##0.00)", "1", "\uFFE51.00", "CNY1.00", "\u4EBA\u6C11\u5E011.00" } };
    String doubleCurrencyStr = "\u00A4\u00A4";
    String tripleCurrencyStr = "\u00A4\u00A4\u00A4";
    for (int i = 0; i < DATA.length; ++i) {
        String locale = DATA[i][0];
        String pat = DATA[i][1];
        Double numberToBeFormat = new Double(DATA[i][2]);
        DecimalFormatSymbols sym = new DecimalFormatSymbols(new ULocale(locale));
        for (int j = 1; j <= 3; ++j) {
            // j represents the number of currency sign in the pattern.
            if (j == 2) {
                pat = pat.replaceAll("\u00A4", doubleCurrencyStr);
            } else if (j == 3) {
                pat = pat.replaceAll("\u00A4\u00A4", tripleCurrencyStr);
            }
            DecimalFormat fmt = new DecimalFormat(pat, sym);
            String s = ((NumberFormat) fmt).format(numberToBeFormat);
            // DATA[i][3] is the currency format result using a
            // single currency sign.
            // DATA[i][4] is the currency format result using
            // double currency sign.
            // DATA[i][5] is the currency format result using
            // triple currency sign.
            // DATA[i][j+2] is the currency format result using
            // 'j' number of currency sign.
            String currencyFormatResult = DATA[i][2 + j];
            if (!s.equals(currencyFormatResult)) {
                errln("FAIL format: Expected " + currencyFormatResult);
            }
            try {
                // mix style parsing
                for (int k = 3; k <= 5; ++k) {
                    // DATA[i][3] is the currency format result using a
                    // single currency sign.
                    // DATA[i][4] is the currency format result using
                    // double currency sign.
                    // DATA[i][5] is the currency format result using
                    // triple currency sign.
                    String oneCurrencyFormat = DATA[i][k];
                    if (fmt.parse(oneCurrencyFormat).doubleValue() != numberToBeFormat.doubleValue()) {
                        errln("FAILED parse " + oneCurrencyFormat);
                    }
                }
            } catch (ParseException e) {
                errln("FAILED, DecimalFormat parse currency: " + e.toString());
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) ParseException(java.text.ParseException) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 68 with NumberFormat

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

the class NumberFormatTest method TestQuotes.

// Test the handling of quotes
@Test
public void TestQuotes() {
    StringBuffer pat;
    DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
    pat = new StringBuffer("a'fo''o'b#");
    DecimalFormat fmt = new DecimalFormat(pat.toString(), sym);
    String s = ((NumberFormat) fmt).format(123);
    logln("Pattern \"" + pat + "\"");
    logln(" Format 123 . " + s);
    if (!s.equals("afo'ob123"))
        errln("FAIL: Expected afo'ob123");
    s = "";
    pat = new StringBuffer("a''b#");
    fmt = new DecimalFormat(pat.toString(), sym);
    s = ((NumberFormat) fmt).format(123);
    logln("Pattern \"" + pat + "\"");
    logln(" Format 123 . " + s);
    if (!s.equals("a'b123"))
        errln("FAIL: Expected a'b123");
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 69 with NumberFormat

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

the class NumberFormatTest method TestFormat.

/*
     * Testing the method public StringBuffer format(Object number, ...)
     */
@Test
public void TestFormat() {
    NumberFormat nf = NumberFormat.getInstance();
    StringBuffer sb = new StringBuffer("dummy");
    FieldPosition fp = new FieldPosition(0);
    // Tests when "if (number instanceof Long)" is true
    try {
        nf.format(new Long("0"), sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "return an exception for a Long object. Error: " + e);
    }
    // Tests when "else if (number instanceof BigInteger)" is true
    try {
        nf.format((Object) new BigInteger("0"), sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "return an exception for a BigInteger object. Error: " + e);
    }
    // Tests when "else if (number instanceof java.math.BigDecimal)" is true
    try {
        nf.format((Object) new java.math.BigDecimal("0"), sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "return an exception for a java.math.BigDecimal object. Error: " + e);
    }
    // Tests when "else if (number instanceof android.icu.math.BigDecimal)" is true
    try {
        nf.format((Object) new android.icu.math.BigDecimal("0"), sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "return an exception for a android.icu.math.BigDecimal object. Error: " + e);
    }
    // Tests when "else if (number instanceof CurrencyAmount)" is true
    try {
        CurrencyAmount ca = new CurrencyAmount(0.0, Currency.getInstance(new ULocale("en_US")));
        nf.format((Object) ca, sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "return an exception for a CurrencyAmount object. Error: " + e);
    }
    // Tests when "else if (number instanceof Number)" is true
    try {
        nf.format(0.0, sb, fp);
    } catch (Exception e) {
        errln("NumberFormat.format(Object number, ...) was not suppose to " + "to return an exception for a Number object. Error: " + e);
    }
    // Tests when "else" is true
    try {
        nf.format(new Object(), sb, fp);
        errln("NumberFormat.format(Object number, ...) was suppose to " + "return an exception for an invalid object.");
    } catch (Exception e) {
    }
    try {
        nf.format(new String("dummy"), sb, fp);
        errln("NumberFormat.format(Object number, ...) was suppose to " + "return an exception for an invalid object.");
    } catch (Exception e) {
    }
}
Also used : ULocale(android.icu.util.ULocale) FieldPosition(java.text.FieldPosition) ParseException(java.text.ParseException) IOException(java.io.IOException) BigDecimal(android.icu.math.BigDecimal) CurrencyAmount(android.icu.util.CurrencyAmount) BigInteger(java.math.BigInteger) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 70 with NumberFormat

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

the class NumberFormatTest method TestFormatAbstractImplCoverage.

/*
     * Coverage tests for the implementation of abstract format methods not being called otherwise
     */
public void TestFormatAbstractImplCoverage() {
    NumberFormat df = DecimalFormat.getInstance(Locale.ENGLISH);
    NumberFormat cdf = CompactDecimalFormat.getInstance(Locale.ENGLISH, CompactDecimalFormat.CompactStyle.SHORT);
    NumberFormat rbf = new RuleBasedNumberFormat(ULocale.ENGLISH, RuleBasedNumberFormat.SPELLOUT);
    /*
         *  Test  NumberFormat.format(BigDecimal,StringBuffer,FieldPosition)
         */
    StringBuffer sb = new StringBuffer();
    String result = df.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
    if (!"2,000.43".equals(result)) {
        errln("DecimalFormat failed. Expected: 2,000.43 - Actual: " + result);
    }
    sb.delete(0, sb.length());
    result = cdf.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
    if (!"2K".equals(result)) {
        errln("DecimalFormat failed. Expected: 2K - Actual: " + result);
    }
    sb.delete(0, sb.length());
    result = rbf.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
    if (!"two thousand point four three".equals(result)) {
        errln("DecimalFormat failed. Expected: 'two thousand point four three' - Actual: '" + result + "'");
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) FieldPosition(java.text.FieldPosition) BigDecimal(android.icu.math.BigDecimal) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat)

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