Java Code Examples for com.ibm.icu.text.NumberFormat#getCurrencyInstance()

The following examples show how to use com.ibm.icu.text.NumberFormat#getCurrencyInstance() . 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: ExcelUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
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 2
Source File: GlobalizationPreferences.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * This function can be overridden by subclasses to use different heuristics.
 * <b>It MUST return a 'safe' value,
 * one whose modification will not affect this object.</b>
 *
 * @param style
 * @draft ICU 3.6
 * @provisional This API might change or be removed in a future release.
 */
protected NumberFormat guessNumberFormat(int style) {
    NumberFormat result;
    ULocale nfLocale = getAvailableLocale(TYPE_NUMBERFORMAT);
    if (nfLocale == null) {
        nfLocale = ULocale.ROOT;
    }
    switch (style) {
    case NF_NUMBER:
        result = NumberFormat.getInstance(nfLocale);
        break;
    case NF_SCIENTIFIC:
        result = NumberFormat.getScientificInstance(nfLocale);
        break;
    case NF_INTEGER:
        result = NumberFormat.getIntegerInstance(nfLocale);
        break;
    case NF_PERCENT:
        result = NumberFormat.getPercentInstance(nfLocale);
        break;
    case NF_CURRENCY:
        result = NumberFormat.getCurrencyInstance(nfLocale);
        result.setCurrency(getCurrency());
        break;
    default:
        throw new IllegalArgumentException("Unknown number format style");
    }
    return result;
}
 
Example 3
Source File: OdsUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
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 4
Source File: FormatCurrencyNumPattern.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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 5
Source File: FormatCurrencyNumPattern.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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 6
Source File: FormatterImpl.java    From org.openntf.domino with Apache License 2.0 4 votes vote down vote up
public String formatNumber(final Number n, final LotusNumberOptions lno) {
	NumberFormat nf;
	/*
	 * It would have been more convenient to use NumberFormat.getInstance(locale, style),
	 * but this method is private in com.ibm.icu_3.8.1.v20120530.jar.
	 * (Seems to be public as of ICU 4.2.)
	 */
	if (lno.format == 'C')
		nf = NumberFormat.getCurrencyInstance(iLocale);
	else if (lno.format == 'S')
		nf = NumberFormat.getScientificInstance(iLocale);
	else if (lno.format == '%')
		nf = NumberFormat.getPercentInstance(iLocale);
	else
		nf = NumberFormat.getNumberInstance(iLocale);
	nf.setGroupingUsed(lno.useGrouping);
	nf.setMaximumIntegerDigits(1000);
	if (lno.fractionDigits != -1) {
		nf.setMinimumFractionDigits(lno.fractionDigits);
		nf.setMaximumFractionDigits(lno.fractionDigits);
	} else
		nf.setMaximumFractionDigits(1000);
	String ret = nf.format(n);
	do {
		if (lno.format != 'G' || ret.length() <= 15)
			break;
		/*
		 * In this case, Lotus implicitly switches to scientific style.
		 * When useGrouping is in effect, the limit decreases from 15 to 12 in Lotus
		 * (i.e. the grouping bytes are likewise counted), but we are not going to
		 *  imitate this strange behaviour.
		 */
		String tester = ret;
		if (lno.useGrouping) {
			nf.setGroupingUsed(false);
			tester = nf.format(n);
		}
		int minus = (tester.charAt(0) == '-') ? 1 : 0;
		int lh = tester.length();
		if (lh - minus <= 15)
			break;
		int komma = minus;
		for (; komma < lh; komma++)
			if (!Character.isDigit(tester.charAt(komma)))
				break;
		if (komma - minus <= 15)
			break;
		nf = NumberFormat.getScientificInstance(iLocale);
		nf.setGroupingUsed(lno.useGrouping);
		ret = nf.format(n);
	} while (false);
	if (lno.negativeAsParentheses && ret.charAt(0) == '-')
		ret = '(' + ret.substring(1) + ')';
	return ret;
}