/** * {@inheritDoc} * @internal ICU 57 technology preview * @deprecated This API might change or be removed in a future release. */ @Override @Deprecated public StringBuffer format(CurrencyAmount currAmt, StringBuffer toAppendTo, FieldPosition pos) { return format(currAmt.getNumber().doubleValue(), currAmt.getCurrency(), toAppendTo, pos); }
/** * Returns the currency of this object. * @return this object's Currency * @stable ICU 3.0 */ public Currency getCurrency() { return (Currency) getUnit(); } }
/** * Parses text from the given string as a CurrencyAmount. Unlike * the parse() method, this method will attempt to parse a generic * currency name, searching for a match of this object's locale's * currency display names, or for a 3-letter ISO currency code. * This method will fail if this format is not a currency format, * that is, if it does not contain the currency pattern symbol * (U+00A4) in its prefix or suffix. * * @param text the text to parse * @param pos input-output position; on input, the position within * text to match; must have 0 <= pos.getIndex() < text.length(); * on output, the position after the last matched character. If * the parse fails, the position in unchanged upon output. * @return a CurrencyAmount, or null upon failure * @stable ICU 49 */ public CurrencyAmount parseCurrency(CharSequence text, ParsePosition pos) { ///CLOVER:OFF // Default implementation only -- subclasses should override Number n = parse(text.toString(), pos); return n == null ? null : new CurrencyAmount(n, getEffectiveCurrency()); ///CLOVER:ON }
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); }
/** * Override Format.format(). * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition) */ public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { if (!(obj instanceof CurrencyAmount)) { throw new IllegalArgumentException("Invalid type: " + obj.getClass().getName()); } CurrencyAmount currency = (CurrencyAmount) obj; fmt.setCurrency(currency.getCurrency()); return fmt.format(currency.getNumber(), toAppendTo, pos); }
@Override public String formatCurrencyAmount(String locale, Number amount, String currencyCode) { ULocale closestLocale = localeService.getClosestULocale(locale); Currency currency; if (StringUtils.isEmpty(currencyCode)) { currency = Currency.getInstance(new ULocale(locale)); } else { currency = Currency.getInstance(currencyCode); } CurrencyAmount ca = new CurrencyAmount(amount, currency); NumberFormat nf = NumberFormat.getCurrencyInstance(closestLocale); nf.setCurrency(currency); return nf.format(ca); }
@Override public StringBuffer format(CurrencyAmount currAmt, StringBuffer toAppendTo, FieldPosition pos) { java.util.Currency save = fJdkNfmt.getCurrency(); String currCode = currAmt.getCurrency().getCurrencyCode(); boolean same = save.getCurrencyCode().equals(currCode); if (!same) { fJdkNfmt.setCurrency(java.util.Currency.getInstance(currCode)); } fJdkNfmt.format(currAmt.getNumber(), toAppendTo, pos); if (!same) { fJdkNfmt.setCurrency(save); } return toAppendTo; }
@Override public String formatCurrencyAmount(String locale, Number amount, String currencyCode) { ULocale closestLocale = findClosestULocale(locale); Currency currency; if (StringUtils.isEmpty(currencyCode)) { currency = Currency.getInstance(new ULocale(locale)); } else { currency = Currency.getInstance(currencyCode); } CurrencyAmount ca = new CurrencyAmount(amount, currency); NumberFormat nf = NumberFormat.getCurrencyInstance(closestLocale); nf.setCurrency(currency); return nf.format(ca); }
/** * {@icu} Formats a CurrencyAmount. Specialization of format. * @see java.text.Format#format(Object, StringBuffer, FieldPosition) * @stable ICU 3.0 */ public StringBuffer format(CurrencyAmount currAmt, StringBuffer toAppendTo, FieldPosition pos) { // Default implementation -- subclasses may override synchronized(this) { Currency save = getCurrency(), curr = currAmt.getCurrency(); boolean same = curr.equals(save); if (!same) setCurrency(curr); format(currAmt.getNumber(), toAppendTo, pos); if (!same) setCurrency(save); } return toAppendTo; }
return (currency != null) ? (Object) new CurrencyAmount(n, currency[0]) : (Object) n;