Search in sources :

Example 1 with Currency

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

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

the class MeasureFormat method formatMeasureRange.

/**
 * Format a range of measures, such as "3.4-5.1 meters". It is the caller’s
 * responsibility to have the appropriate values in appropriate order,
 * and using the appropriate Number values.
 * <br>Note: If the format doesn’t have enough decimals, or lowValue ≥ highValue,
 * the result will be a degenerate range, like “5-5 meters”.
 * <br>Currency Units are not yet supported.
 *
 * @param lowValue low value in range
 * @param highValue high value in range
 * @return the formatted string.
 * @deprecated This API is ICU internal only.
 * @hide original deprecated declaration
 * @hide draft / provisional / internal are hidden on Android
 */
@Deprecated
public final String formatMeasureRange(Measure lowValue, Measure highValue) {
    MeasureUnit unit = lowValue.getUnit();
    if (!unit.equals(highValue.getUnit())) {
        throw new IllegalArgumentException("Units must match: " + unit + " ≠ " + highValue.getUnit());
    }
    Number lowNumber = lowValue.getNumber();
    Number highNumber = highValue.getNumber();
    final boolean isCurrency = unit instanceof Currency;
    UFieldPosition lowFpos = new UFieldPosition();
    UFieldPosition highFpos = new UFieldPosition();
    StringBuffer lowFormatted = null;
    StringBuffer highFormatted = null;
    if (isCurrency) {
        Currency currency = (Currency) unit;
        int fracDigits = currency.getDefaultFractionDigits();
        int maxFrac = numberFormat.nf.getMaximumFractionDigits();
        int minFrac = numberFormat.nf.getMinimumFractionDigits();
        if (fracDigits != maxFrac || fracDigits != minFrac) {
            DecimalFormat currentNumberFormat = (DecimalFormat) numberFormat.get();
            currentNumberFormat.setMaximumFractionDigits(fracDigits);
            currentNumberFormat.setMinimumFractionDigits(fracDigits);
            lowFormatted = currentNumberFormat.format(lowNumber, new StringBuffer(), lowFpos);
            highFormatted = currentNumberFormat.format(highNumber, new StringBuffer(), highFpos);
        }
    }
    if (lowFormatted == null) {
        lowFormatted = numberFormat.format(lowNumber, new StringBuffer(), lowFpos);
        highFormatted = numberFormat.format(highNumber, new StringBuffer(), highFpos);
    }
    final double lowDouble = lowNumber.doubleValue();
    String keywordLow = rules.select(new PluralRules.FixedDecimal(lowDouble, lowFpos.getCountVisibleFractionDigits(), lowFpos.getFractionDigits()));
    final double highDouble = highNumber.doubleValue();
    String keywordHigh = rules.select(new PluralRules.FixedDecimal(highDouble, highFpos.getCountVisibleFractionDigits(), highFpos.getFractionDigits()));
    final PluralRanges pluralRanges = Factory.getDefaultFactory().getPluralRanges(getLocale());
    StandardPlural resolvedPlural = pluralRanges.get(StandardPlural.fromString(keywordLow), StandardPlural.fromString(keywordHigh));
    String rangeFormatter = getRangeFormat(getLocale(), formatWidth);
    String formattedNumber = SimpleFormatterImpl.formatCompiledPattern(rangeFormatter, lowFormatted, highFormatted);
    if (isCurrency) {
        // Nasty hack
        // have to call this for the side effect
        currencyFormat.format(1d);
        Currency currencyUnit = (Currency) unit;
        StringBuilder result = new StringBuilder();
        appendReplacingCurrency(currencyFormat.getPrefix(lowDouble >= 0), currencyUnit, resolvedPlural, result);
        result.append(formattedNumber);
        appendReplacingCurrency(currencyFormat.getSuffix(highDouble >= 0), currencyUnit, resolvedPlural, result);
        return result.toString();
    // StringBuffer buffer = new StringBuffer();
    // CurrencyAmount currencyLow = (CurrencyAmount) lowValue;
    // CurrencyAmount currencyHigh = (CurrencyAmount) highValue;
    // FieldPosition pos = new FieldPosition(NumberFormat.INTEGER_FIELD);
    // currencyFormat.format(currencyLow, buffer, pos);
    // int startOfInteger = pos.getBeginIndex();
    // StringBuffer buffer2 = new StringBuffer();
    // FieldPosition pos2 = new FieldPosition(0);
    // currencyFormat.format(currencyHigh, buffer2, pos2);
    } else {
        String formatter = getPluralFormatter(lowValue.getUnit(), formatWidth, resolvedPlural.ordinal());
        return SimpleFormatterImpl.formatCompiledPattern(formatter, formattedNumber);
    }
}
Also used : MeasureUnit(android.icu.util.MeasureUnit) Currency(android.icu.util.Currency) StandardPlural(android.icu.impl.StandardPlural)

Example 3 with Currency

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

the class DecimalFormat method compareComplexAffix.

/**
 * Returns the length matched by the given affix, or -1 if none.
 *
 * @param affixPat pattern string
 * @param text input text
 * @param pos offset into input at which to begin matching
 * @param type parse against currency type, LONG_NAME only or not.
 * @param currency return value for parsed currency, for generic
 * currency parsing mode, or null for normal parsing.  In generic
 * currency parsing mode, any currency is parsed, not just the
 * currency that this formatter is set to.
 * @return position after the matched text, or -1 if match failure
 */
private int compareComplexAffix(String affixPat, String text, int pos, int type, Currency[] currency) {
    int start = pos;
    for (int i = 0; i < affixPat.length() && pos >= 0; ) {
        char c = affixPat.charAt(i++);
        if (c == QUOTE) {
            for (; ; ) {
                int j = affixPat.indexOf(QUOTE, i);
                if (j == i) {
                    pos = match(text, pos, QUOTE);
                    i = j + 1;
                    break;
                } else if (j > i) {
                    pos = match(text, pos, affixPat.substring(i, j));
                    i = j + 1;
                    if (i < affixPat.length() && affixPat.charAt(i) == QUOTE) {
                        pos = match(text, pos, QUOTE);
                        ++i;
                    // loop again
                    } else {
                        break;
                    }
                } else {
                    // pattern.
                    throw new RuntimeException();
                }
            }
            continue;
        }
        String affix = null;
        switch(c) {
            case CURRENCY_SIGN:
                // since the currency names in choice format is saved the same way as
                // other currency names, do not need to do currency choice parsing here.
                // the general currency parsing parse against all names, including names
                // in choice format.  assert(currency != null || (getCurrency() != null &&
                // currencyChoice != null));
                boolean intl = i < affixPat.length() && affixPat.charAt(i) == CURRENCY_SIGN;
                if (intl) {
                    ++i;
                }
                boolean plural = i < affixPat.length() && affixPat.charAt(i) == CURRENCY_SIGN;
                if (plural) {
                    ++i;
                    intl = false;
                }
                // Parse generic currency -- anything for which we have a display name, or
                // any 3-letter ISO code.  Try to parse display name for our locale; first
                // determine our locale.  TODO: use locale in CurrencyPluralInfo
                ULocale uloc = getLocale(ULocale.VALID_LOCALE);
                if (uloc == null) {
                    // applyPattern has been called; use the symbols
                    uloc = symbols.getLocale(ULocale.VALID_LOCALE);
                }
                // Delegate parse of display name => ISO code to Currency
                ParsePosition ppos = new ParsePosition(pos);
                // using Currency.parse to handle mixed style parsing.
                String iso = Currency.parse(uloc, text, type, ppos);
                // If parse succeeds, populate currency[0]
                if (iso != null) {
                    if (currency != null) {
                        currency[0] = Currency.getInstance(iso);
                    } else {
                        // The formatter is currency-style but the client has not requested
                        // the value of the parsed currency. In this case, if that value does
                        // not match the formatter's current value, then the parse fails.
                        Currency effectiveCurr = getEffectiveCurrency();
                        if (iso.compareTo(effectiveCurr.getCurrencyCode()) != 0) {
                            pos = -1;
                            continue;
                        }
                    }
                    pos = ppos.getIndex();
                } else {
                    pos = -1;
                }
                continue;
            case PATTERN_PERCENT:
                affix = symbols.getPercentString();
                break;
            case PATTERN_PER_MILLE:
                affix = symbols.getPerMillString();
                break;
            case PATTERN_PLUS_SIGN:
                affix = symbols.getPlusSignString();
                break;
            case PATTERN_MINUS_SIGN:
                affix = symbols.getMinusSignString();
                break;
            default:
                // fall through to affix != null test, which will fail
                break;
        }
        if (affix != null) {
            pos = match(text, pos, affix);
            continue;
        }
        pos = match(text, pos, c);
        if (PatternProps.isWhiteSpace(c)) {
            i = skipPatternWhiteSpace(affixPat, i);
        }
    }
    return pos - start;
}
Also used : ULocale(android.icu.util.ULocale) Currency(android.icu.util.Currency) AttributedString(java.text.AttributedString) ParsePosition(java.text.ParsePosition)

Example 4 with Currency

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

the class DecimalFormat method expandAffix.

/**
 * Expands an affix pattern into an affix string. All characters in the pattern are
 * literal unless bracketed by QUOTEs. The following characters outside QUOTE are
 * recognized: PATTERN_PERCENT, PATTERN_PER_MILLE, PATTERN_MINUS, and
 * CURRENCY_SIGN. If CURRENCY_SIGN is doubled, it is interpreted as an international
 * currency sign. If CURRENCY_SIGN is tripled, it is interpreted as currency plural
 * long names, such as "US Dollars". Any other character outside QUOTE represents
 * itself. Quoted text must be well-formed.
 *
 * This method is used in two distinct ways. First, it is used to expand the stored
 * affix patterns into actual affixes. For this usage, doFormat must be false. Second,
 * it is used to expand the stored affix patterns given a specific number (doFormat ==
 * true), for those rare cases in which a currency format references a ChoiceFormat
 * (e.g., en_IN display name for INR). The number itself is taken from digitList.
 * TODO: There are no currency ChoiceFormat patterns, figure out what is still relevant here.
 *
 * When used in the first way, this method has a side effect: It sets currencyChoice
 * to a ChoiceFormat object, if the currency's display name in this locale is a
 * ChoiceFormat pattern (very rare). It only does this if currencyChoice is null to
 * start with.
 *
 * @param pattern the non-null, possibly empty pattern
 * @param pluralCount the plural count. It is only used for currency plural format. In
 * which case, it is the plural count of the currency amount. For example, in en_US,
 * it is the singular "one", or the plural "other". For all other cases, it is null,
 * and is not being used.
 * @param buffer a scratch StringBuffer; its contents will be lost
 */
// Bug 4212072 [Richard/GCL]
private void expandAffix(String pattern, String pluralCount, StringBuffer buffer) {
    buffer.setLength(0);
    for (int i = 0; i < pattern.length(); ) {
        char c = pattern.charAt(i++);
        if (c == QUOTE) {
            for (; ; ) {
                int j = pattern.indexOf(QUOTE, i);
                if (j == i) {
                    buffer.append(QUOTE);
                    i = j + 1;
                    break;
                } else if (j > i) {
                    buffer.append(pattern.substring(i, j));
                    i = j + 1;
                    if (i < pattern.length() && pattern.charAt(i) == QUOTE) {
                        buffer.append(QUOTE);
                        ++i;
                    // loop again
                    } else {
                        break;
                    }
                } else {
                    // pattern.
                    throw new RuntimeException();
                }
            }
            continue;
        }
        switch(c) {
            case CURRENCY_SIGN:
                // As of ICU 2.2 we use the currency object, and ignore the currency
                // symbols in the DFS, unless we have a null currency object. This occurs
                // if resurrecting a pre-2.2 object or if the user sets a custom DFS.
                boolean intl = i < pattern.length() && pattern.charAt(i) == CURRENCY_SIGN;
                boolean plural = false;
                if (intl) {
                    ++i;
                    if (i < pattern.length() && pattern.charAt(i) == CURRENCY_SIGN) {
                        plural = true;
                        intl = false;
                        ++i;
                    }
                }
                String s = null;
                Currency currency = getCurrency();
                if (currency != null) {
                    // pluralCount == null, and plural names are not needed.
                    if (plural && pluralCount != null) {
                        s = currency.getName(symbols.getULocale(), Currency.PLURAL_LONG_NAME, pluralCount, null);
                    } else if (!intl) {
                        s = currency.getName(symbols.getULocale(), Currency.SYMBOL_NAME, null);
                    } else {
                        s = currency.getCurrencyCode();
                    }
                } else {
                    s = intl ? symbols.getInternationalCurrencySymbol() : symbols.getCurrencySymbol();
                }
                // Here is where FieldPosition could be set for CURRENCY PLURAL.
                buffer.append(s);
                break;
            case PATTERN_PERCENT:
                buffer.append(symbols.getPercentString());
                break;
            case PATTERN_PER_MILLE:
                buffer.append(symbols.getPerMillString());
                break;
            case PATTERN_MINUS_SIGN:
                buffer.append(symbols.getMinusSignString());
                break;
            default:
                buffer.append(c);
                break;
        }
    }
}
Also used : Currency(android.icu.util.Currency) AttributedString(java.text.AttributedString)

Example 5 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)

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