com.ibm.icu.util.Currency Java Examples
The following examples show how to use
com.ibm.icu.util.Currency.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MeasureFormat.java From fitnotifications with Apache License 2.0 | 6 votes |
private void appendReplacingCurrency(String affix, Currency unit, StandardPlural resolvedPlural, StringBuilder result) { String replacement = "¤"; int pos = affix.indexOf(replacement); if (pos < 0) { replacement = "XXX"; pos = affix.indexOf(replacement); } if (pos < 0) { result.append(affix); } else { // for now, just assume single result.append(affix.substring(0,pos)); // we have a mismatch between the number style and the currency style, so remap int currentStyle = formatWidth.getCurrencyStyle(); if (currentStyle == NumberFormat.ISOCURRENCYSTYLE) { result.append(unit.getCurrencyCode()); } else { result.append(unit.getName(currencyFormat.nf.getLocale(ULocale.ACTUAL_LOCALE), currentStyle == NumberFormat.CURRENCYSTYLE ? Currency.SYMBOL_NAME : Currency.PLURAL_LONG_NAME, resolvedPlural.getKeyword(), null)); } result.append(affix.substring(pos+replacement.length())); } }
Example #2
Source File: MeasureFormat.java From fitnotifications with Apache License 2.0 | 6 votes |
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); }
Example #3
Source File: FormatCurrencyNumPattern.java From birt with Eclipse Public License 1.0 | 6 votes |
/** * Returns the default number of fraction digits that should be displayed * for the default currency for given locale. * * @param locale * @return */ public static int getDefaultFractionDigits( ULocale locale ) { if ( locale == null ) { locale = ULocale.getDefault( ); } Currency currency = Currency.getInstance( locale ); if ( currency != null ) { return currency.getDefaultFractionDigits( ); } return 2; }
Example #4
Source File: ExcelUtil.java From birt with Eclipse Public License 1.0 | 6 votes |
private static String getCurrencySymbol( ULocale locale ) { NumberFormat format = NumberFormat.getCurrencyInstance( locale ); Currency currency = format.getCurrency( ); if ( currency != null ) { String symbol = currency.getSymbol( locale ); if ( symbol.equals( "EUR" ) ) { symbol = "\u20ac"; // "€"; } else if ( symbol.equals( "GBP" ) ) { symbol = "\u00a3"; // "£"; } else if ( symbol.equals( "XXX" ) ) { symbol = "\u00a4"; // "¤"; } return symbol; } return "$"; }
Example #5
Source File: NumberFormatServiceShim.java From fitnotifications with Apache License 2.0 | 5 votes |
NumberFormat createInstance(ULocale desiredLocale, int choice) { // use service cache // if (service.isDefault()) { // return NumberFormat.createInstance(desiredLocale, choice); // } ULocale[] actualLoc = new ULocale[1]; NumberFormat fmt = (NumberFormat)service.get(desiredLocale, choice, actualLoc); if (fmt == null) { throw new MissingResourceException("Unable to construct NumberFormat", "", ""); } fmt = (NumberFormat)fmt.clone(); // If we are creating a currency type formatter, then we may have to set the currency // explicitly, since the actualLoc may be different than the desiredLocale if ( choice == NumberFormat.CURRENCYSTYLE || choice == NumberFormat.ISOCURRENCYSTYLE || choice == NumberFormat.PLURALCURRENCYSTYLE) { fmt.setCurrency(Currency.getInstance(desiredLocale)); } ULocale uloc = actualLoc[0]; fmt.setLocale(uloc, uloc); // services make no distinction between actual & valid return fmt; }
Example #6
Source File: NumberFormat.java From fitnotifications with Apache License 2.0 | 5 votes |
/** * {@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; }
Example #7
Source File: NumberFormatObject.java From es6draft with MIT License | 5 votes |
private NumberFormat createNumberFormat() { ULocale locale = ULocale.forLanguageTag(this.locale); int choice; if ("decimal".equals(style)) { choice = NumberFormat.NUMBERSTYLE; } else if ("percent".equals(style)) { choice = NumberFormat.PERCENTSTYLE; } else { if ("code".equals(currencyDisplay)) { choice = NumberFormat.ISOCURRENCYSTYLE; } else if ("symbol".equals(currencyDisplay)) { choice = NumberFormat.CURRENCYSTYLE; } else { choice = NumberFormat.PLURALCURRENCYSTYLE; } } DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale, choice); if ("currency".equals(style)) { numberFormat.setCurrency(Currency.getInstance(currency)); } // numberingSystem is already handled in language-tag // assert locale.getKeywordValue("numbers").equals(numberingSystem); if (minimumSignificantDigits != 0 && maximumSignificantDigits != 0) { numberFormat.setSignificantDigitsUsed(true); numberFormat.setMinimumSignificantDigits(minimumSignificantDigits); numberFormat.setMaximumSignificantDigits(maximumSignificantDigits); } else { numberFormat.setSignificantDigitsUsed(false); numberFormat.setMinimumIntegerDigits(minimumIntegerDigits); numberFormat.setMinimumFractionDigits(minimumFractionDigits); numberFormat.setMaximumFractionDigits(maximumFractionDigits); } numberFormat.setGroupingUsed(useGrouping); // as required by ToRawPrecision/ToRawFixed // FIXME: ICU4J bug: // new Intl.NumberFormat("en",{useGrouping:false}).format(111111111111111) // returns "111111111111111.02" numberFormat.setRoundingMode(BigDecimal.ROUND_HALF_UP); return numberFormat; }
Example #8
Source File: NumberFormat.java From fitnotifications with Apache License 2.0 | 5 votes |
/** * Returns the currency in effect for this formatter. Subclasses * should override this method as needed. Unlike getCurrency(), * this method should never return null. * @return a non-null Currency * @internal * @deprecated This API is ICU internal only. */ @Deprecated protected Currency getEffectiveCurrency() { Currency c = getCurrency(); if (c == null) { ULocale uloc = getLocale(ULocale.VALID_LOCALE); if (uloc == null) { uloc = ULocale.getDefault(Category.FORMAT); } c = Currency.getInstance(uloc); } return c; }
Example #9
Source File: FormatCurrencyNumPattern.java From birt with Eclipse Public License 1.0 | 5 votes |
/** * Returns the default currency symbol position for given locale. Returns * <code>null</code> if no symbol needed by default. * * @param locale * @return */ public static String getDefaultSymbolPosition( ULocale locale ) { if ( locale == null ) { locale = ULocale.getDefault( ); } Currency currency = Currency.getInstance( locale ); if ( currency != null ) { String symbol = currency.getSymbol( ); if ( symbol == null ) { return null; } NumberFormat formater = NumberFormat.getCurrencyInstance( locale ); String result = formater.format( 1 ); if ( result.endsWith( symbol ) ) { return FormatNumberPattern.SYMBOL_POSITION_AFTER; } else { return FormatNumberPattern.SYMBOL_POSITION_BEFORE; } } return null; }
Example #10
Source File: OdsUtil.java From birt with Eclipse Public License 1.0 | 5 votes |
private static String getCurrencySymbol( ULocale locale ) { NumberFormat format = NumberFormat.getCurrencyInstance( locale ); Currency currency = format.getCurrency( ); if ( currency != null ) { String symbol = currency.getSymbol( locale ); if ( symbol.equals( "EUR" ) ) { symbol = "€"; } if ( symbol.equals( "GBP" ) ) { symbol = "£"; } if ( symbol.equals( "XXX" ) ) { symbol = "¤"; } if ( symbol == null ) { symbol = "$"; } return symbol; } return "$"; }
Example #11
Source File: CompactDecimalFormat.java From fitnotifications with Apache License 2.0 | 5 votes |
private Amount toAmount(double number, Currency curr, Output<Unit> currencyUnit) { // We do this here so that the prefix or suffix we choose is always consistent // with the rounding we do. This way, 999999 -> 1M instead of 1000K. boolean negative = isNumberNegative(number); number = adjustNumberAsInFormatting(number); int base = number <= 1.0d ? 0 : (int) Math.log10(number); if (base >= CompactDecimalDataCache.MAX_DIGITS) { base = CompactDecimalDataCache.MAX_DIGITS - 1; } if (curr != null) { number /= currencyDivisor[base]; } else { number /= divisor[base]; } String pluralVariant = getPluralForm(getFixedDecimal(number, toDigitList(number))); if (pluralToCurrencyAffixes != null && currencyUnit != null) { currencyUnit.value = pluralToCurrencyAffixes.get(pluralVariant); } if (negative) { number = -number; } if ( curr != null ) { return new Amount(number, CompactDecimalDataCache.getUnit(currencyUnits, pluralVariant, base)); } else { return new Amount(number, CompactDecimalDataCache.getUnit(units, pluralVariant, base)); } }
Example #12
Source File: MeasureFormat.java From fitnotifications with Apache License 2.0 | 4 votes |
/** * 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. * @internal * @deprecated This API is ICU internal only. */ @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 currencyFormat.format(1d); // have to call this for the side effect 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); } }
Example #13
Source File: FormatCurrencyNumPattern.java From birt with Eclipse Public License 1.0 | 4 votes |
/** * Returns the if symbol space is used by default for given locale. * * @param locale * @return */ public static boolean getDefaultUsingSymbolSpace( ULocale locale ) { if ( locale == null ) { locale = ULocale.getDefault( ); } Currency currency = Currency.getInstance( locale ); if ( currency != null ) { String symbol = currency.getSymbol( ); if ( symbol == null ) { return false; } NumberFormat formater = NumberFormat.getCurrencyInstance( locale ); String result = formater.format( 1 ); if ( result.endsWith( symbol ) ) { result = result.substring( 0, result.indexOf( symbol ) ); for ( int i = result.length( ) - 1; i >= 0; i-- ) { if ( UCharacter.isSpaceChar( result.codePointAt( i ) ) ) { return true; } } } else { result = result.substring( result.indexOf( symbol ) + symbol.length( ) ); for ( int i = 0; i < result.length( ); i++ ) { if ( UCharacter.isSpaceChar( result.codePointAt( i ) ) ) { return true; } } } } return false; }
Example #14
Source File: CompactDecimalFormat.java From fitnotifications with Apache License 2.0 | 4 votes |
private StringBuffer format(double number, Currency curr, StringBuffer toAppendTo, FieldPosition pos) { if (curr != null && style == CompactStyle.LONG) { throw new UnsupportedOperationException("CompactDecimalFormat does not support LONG style for currency."); } // Compute the scaled amount, prefix, and suffix appropriate for the number's magnitude. Output<Unit> currencyUnit = new Output<Unit>(); Amount amount = toAmount(number, curr, currencyUnit); Unit unit = amount.getUnit(); // Note that currencyUnit is a remnant. In almost all cases, it will be null. StringBuffer prefix = new StringBuffer(); StringBuffer suffix = new StringBuffer(); if (currencyUnit.value != null) { currencyUnit.value.writePrefix(prefix); } unit.writePrefix(prefix); unit.writeSuffix(suffix); if (currencyUnit.value != null) { currencyUnit.value.writeSuffix(suffix); } if (curr == null) { // Prevent locking when not formatting a currency number. toAppendTo.append(escape(prefix.toString())); super.format(amount.getQty(), toAppendTo, pos); toAppendTo.append(escape(suffix.toString())); } else { // To perform the formatting, we set this DecimalFormat's pattern to have the correct prefix, suffix, // and currency, and then reset it back to what it was before. // This has to be synchronized since this information is held in the state of the DecimalFormat object. synchronized(this) { String originalPattern = this.toPattern(); Currency originalCurrency = this.getCurrency(); StringBuffer newPattern = new StringBuffer(); // Write prefixes and suffixes to the pattern. Note that we have to apply it to both halves of a // positive/negative format (separated by ';') int semicolonPos = originalPattern.indexOf(';'); newPattern.append(prefix); if (semicolonPos != -1) { newPattern.append(originalPattern, 0, semicolonPos); newPattern.append(suffix); newPattern.append(';'); newPattern.append(prefix); } newPattern.append(originalPattern, semicolonPos + 1, originalPattern.length()); newPattern.append(suffix); // Overwrite the pattern and currency. setCurrency(curr); applyPattern(newPattern.toString()); // Actually perform the formatting. super.format(amount.getQty(), toAppendTo, pos); // Reset the pattern and currency. setCurrency(originalCurrency); applyPattern(originalPattern); } } return toAppendTo; }
Example #15
Source File: DecimalFormatSymbols.java From fitnotifications with Apache License 2.0 | 4 votes |
/** * Initializes the symbols from the locale data. */ private void initialize( ULocale locale ) { this.requestedLocale = locale.toLocale(); this.ulocale = locale; CacheData data = cachedLocaleData.getInstance(locale, null /* unused */); setLocale(data.validLocale, data.validLocale); setDigitStrings(data.digits); String[] numberElements = data.numberElements; // Copy data from the numberElements map into instance fields setDecimalSeparatorString(numberElements[0]); setGroupingSeparatorString(numberElements[1]); // See CLDR #9781 // assert numberElements[2].length() == 1; patternSeparator = numberElements[2].charAt(0); setPercentString(numberElements[3]); setMinusSignString(numberElements[4]); setPlusSignString(numberElements[5]); setExponentSeparator(numberElements[6]); setPerMillString(numberElements[7]); setInfinity(numberElements[8]); setNaN(numberElements[9]); setMonetaryDecimalSeparatorString(numberElements[10]); setMonetaryGroupingSeparatorString(numberElements[11]); setExponentMultiplicationSign(numberElements[12]); digit = DecimalFormat.PATTERN_DIGIT; // Localized pattern character no longer in CLDR padEscape = DecimalFormat.PATTERN_PAD_ESCAPE; sigDigit = DecimalFormat.PATTERN_SIGNIFICANT_DIGIT; CurrencyDisplayInfo info = CurrencyData.provider.getInstance(locale, true); // Obtain currency data from the currency API. This is strictly // for backward compatibility; we don't use DecimalFormatSymbols // for currency data anymore. currency = Currency.getInstance(locale); if (currency != null) { intlCurrencySymbol = currency.getCurrencyCode(); currencySymbol = currency.getName(locale, Currency.SYMBOL_NAME, null); CurrencyFormatInfo fmtInfo = info.getFormatInfo(intlCurrencySymbol); if (fmtInfo != null) { currencyPattern = fmtInfo.currencyPattern; setMonetaryDecimalSeparatorString(fmtInfo.monetarySeparator); setMonetaryGroupingSeparatorString(fmtInfo.monetaryGroupingSeparator); } } else { intlCurrencySymbol = "XXX"; currencySymbol = "\u00A4"; // 'OX' currency symbol } // Get currency spacing data. initSpacingInfo(info.getSpacingInfo()); }
Example #16
Source File: DecimalFormatSymbols.java From fitnotifications with Apache License 2.0 | 3 votes |
/** * Sets the currency. * * <p><strong>Note:</strong> ICU does not use the DecimalFormatSymbols for the currency * any more. This API is present for API compatibility only. * * <p>This also sets the currency symbol attribute to the currency's symbol * in the DecimalFormatSymbols' locale, and the international currency * symbol attribute to the currency's ISO 4217 currency code. * * @param currency the new currency to be used * @throws NullPointerException if <code>currency</code> is null * @see #setCurrencySymbol * @see #setInternationalCurrencySymbol * * @stable ICU 3.4 */ public void setCurrency(Currency currency) { if (currency == null) { throw new NullPointerException(); } this.currency = currency; intlCurrencySymbol = currency.getCurrencyCode(); currencySymbol = currency.getSymbol(requestedLocale); }
Example #17
Source File: DecimalFormatSymbols.java From fitnotifications with Apache License 2.0 | 2 votes |
/** * Returns the currency symbol, for {@link DecimalFormatSymbols#getCurrency()} API * compatibility only. ICU clients should use the Currency API directly. * @return the currency used, or null * @stable ICU 3.4 */ public Currency getCurrency() { return currency; }
Example #18
Source File: NumberFormat.java From fitnotifications with Apache License 2.0 | 2 votes |
/** * Returns the <tt>Currency</tt> object used to display currency * amounts. This may be null. * @stable ICU 2.6 */ public Currency getCurrency() { return currency; }
Example #19
Source File: NumberFormat.java From fitnotifications with Apache License 2.0 | 2 votes |
/** * Sets the <tt>Currency</tt> object used to display currency * amounts. This takes effect immediately, if this format is a * currency format. If this format is not a currency format, then * the currency object is used if and when this object becomes a * currency format. * @param theCurrency new currency object to use. May be null for * some subclasses. * @stable ICU 2.6 */ public void setCurrency(Currency theCurrency) { currency = theCurrency; }