Search in sources :

Example 11 with Currency

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

the class DecimalFormat method setCurrencyUsage.

/**
 * Sets the <tt>Currency Usage</tt> object used to display currency.
 * This takes effect immediately, if this format is a
 * currency format.
 * @param newUsage new currency context object to use.
 */
public void setCurrencyUsage(CurrencyUsage newUsage) {
    if (newUsage == null) {
        throw new NullPointerException("return value is null at method AAA");
    }
    currencyUsage = newUsage;
    Currency theCurrency = this.getCurrency();
    // We set rounding/digit based on currency context
    if (theCurrency != null) {
        setRoundingIncrement(theCurrency.getRoundingIncrement(currencyUsage));
        int d = theCurrency.getDefaultFractionDigits(currencyUsage);
        setMinimumFractionDigits(d);
        _setMaximumFractionDigits(d);
    }
}
Also used : Currency(android.icu.util.Currency)

Example 12 with Currency

use of android.icu.util.Currency 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)

Example 13 with Currency

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

the class NumberFormatTest method TestJB3832.

@Test
public void TestJB3832() {
    ULocale locale = new ULocale("pt_PT@currency=PTE");
    NumberFormat format = NumberFormat.getCurrencyInstance(locale);
    Currency curr = Currency.getInstance(locale);
    logln("\nName of the currency is: " + curr.getName(locale, Currency.LONG_NAME, new boolean[] { false }));
    CurrencyAmount cAmt = new CurrencyAmount(1150.50, curr);
    // cover hashCode
    logln("CurrencyAmount object's hashCode is: " + cAmt.hashCode());
    String str = format.format(cAmt);
    String expected = "1,150$50\u00a0\u200b";
    if (!expected.equals(str)) {
        errln("Did not get the expected output Expected: " + expected + " Got: " + str);
    }
}
Also used : ULocale(android.icu.util.ULocale) Currency(android.icu.util.Currency) CurrencyAmount(android.icu.util.CurrencyAmount) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 14 with Currency

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

the class NumberFormatTest method TestSetCurrency.

@Test
public void TestSetCurrency() {
    DecimalFormatSymbols decf1 = DecimalFormatSymbols.getInstance(ULocale.US);
    DecimalFormatSymbols decf2 = DecimalFormatSymbols.getInstance(ULocale.US);
    decf2.setCurrencySymbol("UKD");
    DecimalFormat format1 = new DecimalFormat("000.000", decf1);
    DecimalFormat format2 = new DecimalFormat("000.000", decf2);
    Currency euro = Currency.getInstance("EUR");
    format1.setCurrency(euro);
    format2.setCurrency(euro);
    assertEquals("Reset with currency symbol", format1, format2);
}
Also used : DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) Currency(android.icu.util.Currency) Test(org.junit.Test)

Example 15 with Currency

use of android.icu.util.Currency 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)

Aggregations

Currency (android.icu.util.Currency)28 Test (org.junit.Test)19 ULocale (android.icu.util.ULocale)13 Locale (java.util.Locale)7 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)4 NumberFormat (android.icu.text.NumberFormat)4 RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)4 CurrencyAmount (android.icu.util.CurrencyAmount)4 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)3 DecimalFormat (android.icu.text.DecimalFormat)3 AttributedString (java.text.AttributedString)3 StandardPlural (android.icu.impl.StandardPlural)2 MeasureUnit (android.icu.util.MeasureUnit)2 BigDecimal (android.icu.math.BigDecimal)1 MeasureFormat (android.icu.text.MeasureFormat)1 GlobalizationPreferences (android.icu.util.GlobalizationPreferences)1 Output (android.icu.util.Output)1 ParseException (java.text.ParseException)1 ParsePosition (java.text.ParsePosition)1