Search in sources :

Example 1 with CurrencyAmount

use of android.icu.util.CurrencyAmount in project j2objc by google.

the class CompactDecimalFormatTest method TestBug12689.

@Test
public void TestBug12689() {
    if (logKnownIssue("12689", "CDF fails for numbers less than 1 thousand in most locales")) {
        return;
    }
    CompactDecimalFormat cdf;
    String result;
    cdf = CompactDecimalFormat.getInstance(ULocale.forLanguageTag("en"), CompactStyle.SHORT);
    result = cdf.format(new CurrencyAmount(123, Currency.getInstance("USD")));
    assertEquals("CDF should correctly format 123 with currency in 'en'", "$120", result);
    cdf = CompactDecimalFormat.getInstance(ULocale.forLanguageTag("it"), CompactStyle.SHORT);
    result = cdf.format(new CurrencyAmount(123, Currency.getInstance("EUR")));
    assertEquals("CDF should correctly format 123 with currency in 'it'", "120 €", result);
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) CurrencyAmount(android.icu.util.CurrencyAmount) Test(org.junit.Test)

Example 2 with CurrencyAmount

use of android.icu.util.CurrencyAmount in project j2objc by google.

the class CompactDecimalFormatTest method TestBug12688.

@Test
public void TestBug12688() {
    if (logKnownIssue("12688", "CDF fails for numbers less than 1 million in 'it'")) {
        return;
    }
    CompactDecimalFormat cdf;
    String result;
    cdf = CompactDecimalFormat.getInstance(ULocale.forLanguageTag("it"), CompactStyle.SHORT);
    result = cdf.format(new CurrencyAmount(123000, Currency.getInstance("EUR")));
    assertEquals("CDF should correctly format 123000 with currency in 'it'", "120000 €", result);
}
Also used : CompactDecimalFormat(android.icu.text.CompactDecimalFormat) CurrencyAmount(android.icu.util.CurrencyAmount) Test(org.junit.Test)

Example 3 with CurrencyAmount

use of android.icu.util.CurrencyAmount in project j2objc by google.

the class MeasureFormat method formatMeasure.

private StringBuilder formatMeasure(Measure measure, ImmutableNumberFormat nf, StringBuilder appendTo, FieldPosition fieldPosition) {
    Number n = measure.getNumber();
    MeasureUnit unit = measure.getUnit();
    if (unit instanceof Currency) {
        return appendTo.append(currencyFormat.format(new CurrencyAmount(n, (Currency) unit), new StringBuffer(), fieldPosition));
    }
    StringBuffer formattedNumber = new StringBuffer();
    StandardPlural pluralForm = QuantityFormatter.selectPlural(n, nf.nf, rules, formattedNumber, fieldPosition);
    String formatter = getPluralFormatter(unit, formatWidth, pluralForm.ordinal());
    return QuantityFormatter.format(formatter, formattedNumber, appendTo, fieldPosition);
}
Also used : MeasureUnit(android.icu.util.MeasureUnit) Currency(android.icu.util.Currency) StandardPlural(android.icu.impl.StandardPlural) CurrencyAmount(android.icu.util.CurrencyAmount)

Example 4 with CurrencyAmount

use of android.icu.util.CurrencyAmount in project j2objc by google.

the class NumberFormatTest method TestParseCurrency.

@Test
public void TestParseCurrency() {
    class ParseCurrencyItem {

        private final String localeString;

        private final String descrip;

        private final String currStr;

        private final int numExpectPos;

        private final int numExpectVal;

        private final int curExpectPos;

        private final int curExpectVal;

        private final String curExpectCurr;

        ParseCurrencyItem(String locStr, String desc, String curr, int numExPos, int numExVal, int curExPos, int curExVal, String curExCurr) {
            localeString = locStr;
            descrip = desc;
            currStr = curr;
            numExpectPos = numExPos;
            numExpectVal = numExVal;
            curExpectPos = curExPos;
            curExpectVal = curExVal;
            curExpectCurr = curExCurr;
        }

        public String getLocaleString() {
            return localeString;
        }

        public String getDescrip() {
            return descrip;
        }

        public String getCurrStr() {
            return currStr;
        }

        public int getNumExpectPos() {
            return numExpectPos;
        }

        public int getNumExpectVal() {
            return numExpectVal;
        }

        public int getCurExpectPos() {
            return curExpectPos;
        }

        public int getCurExpectVal() {
            return curExpectVal;
        }

        public String getCurExpectCurr() {
            return curExpectCurr;
        }
    }
    final ParseCurrencyItem[] parseCurrencyItems = { new ParseCurrencyItem("en_US", "dollars2", "$2.00", 5, 2, 5, 2, "USD"), new ParseCurrencyItem("en_US", "dollars4", "$4", 2, 4, 2, 4, "USD"), new ParseCurrencyItem("en_US", "dollars9", "9\u00A0$", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_US", "pounds3", "\u00A33.00", 0, 0, 5, 3, "GBP"), new ParseCurrencyItem("en_US", "pounds5", "\u00A35", 0, 0, 2, 5, "GBP"), new ParseCurrencyItem("en_US", "pounds7", "7\u00A0\u00A3", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_US", "euros8", "\u20AC8", 0, 0, 2, 8, "EUR"), new ParseCurrencyItem("en_GB", "pounds3", "\u00A33.00", 5, 3, 5, 3, "GBP"), new ParseCurrencyItem("en_GB", "pounds5", "\u00A35", 2, 5, 2, 5, "GBP"), new ParseCurrencyItem("en_GB", "pounds7", "7\u00A0\u00A3", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_GB", "euros4", "4,00\u00A0\u20AC", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_GB", "euros6", "6\u00A0\u20AC", 0, 0, 0, 0, ""), new ParseCurrencyItem("en_GB", "euros8", "\u20AC8", 0, 0, 2, 8, "EUR"), new ParseCurrencyItem("en_GB", "dollars4", "US$4", 0, 0, 4, 4, "USD"), new ParseCurrencyItem("fr_FR", "euros4", "4,00\u00A0\u20AC", 6, 4, 6, 4, "EUR"), new ParseCurrencyItem("fr_FR", "euros6", "6\u00A0\u20AC", 3, 6, 3, 6, "EUR"), new ParseCurrencyItem("fr_FR", "euros8", "\u20AC8", 0, 0, 0, 0, ""), new ParseCurrencyItem("fr_FR", "dollars2", "$2.00", 0, 0, 0, 0, ""), new ParseCurrencyItem("fr_FR", "dollars4", "$4", 0, 0, 0, 0, "") };
    for (ParseCurrencyItem item : parseCurrencyItems) {
        String localeString = item.getLocaleString();
        ULocale uloc = new ULocale(localeString);
        NumberFormat fmt = null;
        try {
            fmt = NumberFormat.getCurrencyInstance(uloc);
        } catch (Exception e) {
            errln("NumberFormat.getCurrencyInstance fails for locale " + localeString);
            continue;
        }
        String currStr = item.getCurrStr();
        ParsePosition parsePos = new ParsePosition(0);
        Number numVal = fmt.parse(currStr, parsePos);
        if (parsePos.getIndex() != item.getNumExpectPos() || (numVal != null && numVal.intValue() != item.getNumExpectVal())) {
            if (numVal != null) {
                errln("NumberFormat.getCurrencyInstance parse " + localeString + "/" + item.getDescrip() + ", expect pos/val " + item.getNumExpectPos() + "/" + item.getNumExpectVal() + ", get " + parsePos.getIndex() + "/" + numVal.intValue());
            } else {
                errln("NumberFormat.getCurrencyInstance parse " + localeString + "/" + item.getDescrip() + ", expect pos/val " + item.getNumExpectPos() + "/" + item.getNumExpectVal() + ", get " + parsePos.getIndex() + "/(NULL)");
            }
        }
        parsePos.setIndex(0);
        CurrencyAmount currAmt = fmt.parseCurrency(currStr, parsePos);
        if (parsePos.getIndex() != item.getCurExpectPos() || (currAmt != null && (currAmt.getNumber().intValue() != item.getCurExpectVal() || currAmt.getCurrency().getCurrencyCode().compareTo(item.getCurExpectCurr()) != 0))) {
            if (currAmt != null) {
                errln("NumberFormat.getCurrencyInstance parseCurrency " + localeString + "/" + item.getDescrip() + ", expect pos/val/curr " + item.getCurExpectPos() + "/" + item.getCurExpectVal() + "/" + item.getCurExpectCurr() + ", get " + parsePos.getIndex() + "/" + currAmt.getNumber().intValue() + "/" + currAmt.getCurrency().getCurrencyCode());
            } else {
                errln("NumberFormat.getCurrencyInstance parseCurrency " + localeString + "/" + item.getDescrip() + ", expect pos/val/curr " + item.getCurExpectPos() + "/" + item.getCurExpectVal() + "/" + item.getCurExpectCurr() + ", get " + parsePos.getIndex() + "/(NULL)");
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) ParseException(java.text.ParseException) IOException(java.io.IOException) CurrencyAmount(android.icu.util.CurrencyAmount) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 5 with CurrencyAmount

use of android.icu.util.CurrencyAmount in project j2objc by google.

the class NumberFormatTest method TestCurrency.

/**
 * Test localized currency patterns.
 */
@Test
public void TestCurrency() {
    String[] DATA = { "fr", "CA", "", "1,50\u00a0$", "de", "DE", "", "1,50\u00a0\u20AC", "de", "DE", "PREEURO", "1,50\u00a0DM", "fr", "FR", "", "1,50\u00a0\u20AC", "fr", "FR", "PREEURO", "1,50\u00a0F" };
    for (int i = 0; i < DATA.length; i += 4) {
        Locale locale = new Locale(DATA[i], DATA[i + 1], DATA[i + 2]);
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        String s = fmt.format(1.50);
        if (s.equals(DATA[i + 3])) {
            logln("Ok: 1.50 x " + locale + " => " + s);
        } else {
            logln("FAIL: 1.50 x " + locale + " => " + s + ", expected " + DATA[i + 3]);
        }
    }
    // format currency with CurrencyAmount
    for (int i = 0; i < DATA.length; i += 4) {
        Locale locale = new Locale(DATA[i], DATA[i + 1], DATA[i + 2]);
        Currency curr = Currency.getInstance(locale);
        logln("\nName of the currency is: " + curr.getName(locale, Currency.LONG_NAME, new boolean[] { false }));
        CurrencyAmount cAmt = new CurrencyAmount(1.5, curr);
        // cover hashCode
        logln("CurrencyAmount object's hashCode is: " + cAmt.hashCode());
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        String sCurr = fmt.format(cAmt);
        if (sCurr.equals(DATA[i + 3])) {
            logln("Ok: 1.50 x " + locale + " => " + sCurr);
        } else {
            errln("FAIL: 1.50 x " + locale + " => " + sCurr + ", expected " + DATA[i + 3]);
        }
    }
    // Cover MeasureFormat.getCurrencyFormat()
    ULocale save = ULocale.getDefault();
    ULocale.setDefault(ULocale.US);
    MeasureFormat curFmt = MeasureFormat.getCurrencyFormat();
    String strBuf = curFmt.format(new CurrencyAmount(new Float(1234.56), Currency.getInstance("USD")));
    try {
        CurrencyAmount parsedVal = (CurrencyAmount) curFmt.parseObject(strBuf);
        Number val = parsedVal.getNumber();
        if (!val.equals(new BigDecimal("1234.56"))) {
            errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the number. val=" + val);
        }
        if (!parsedVal.getCurrency().equals(Currency.getInstance("USD"))) {
            errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the currency");
        }
    } catch (ParseException e) {
        errln("FAIL: " + e.getMessage());
    }
    ULocale.setDefault(save);
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ULocale(android.icu.util.ULocale) CurrencyAmount(android.icu.util.CurrencyAmount) BigDecimal(android.icu.math.BigDecimal) Currency(android.icu.util.Currency) ParseException(java.text.ParseException) MeasureFormat(android.icu.text.MeasureFormat) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Aggregations

CurrencyAmount (android.icu.util.CurrencyAmount)14 Test (org.junit.Test)10 ULocale (android.icu.util.ULocale)8 ParseException (java.text.ParseException)5 BigDecimal (android.icu.math.BigDecimal)4 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)4 NumberFormat (android.icu.text.NumberFormat)4 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)4 MeasureFormat (android.icu.text.MeasureFormat)3 Currency (android.icu.util.Currency)3 IOException (java.io.IOException)3 BigInteger (java.math.BigInteger)3 ParsePosition (java.text.ParsePosition)2 Locale (java.util.Locale)2 StandardPlural (android.icu.impl.StandardPlural)1 ResourceReader (android.icu.impl.data.ResourceReader)1 TokenIterator (android.icu.impl.data.TokenIterator)1 DecimalFormat (android.icu.text.DecimalFormat)1 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)1 MeasureUnit (android.icu.util.MeasureUnit)1