Java Code Examples for java.text.DecimalFormatSymbols#setInternationalCurrencySymbol()

The following examples show how to use java.text.DecimalFormatSymbols#setInternationalCurrencySymbol() . 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: Helper.java    From smartcoins-wallet with MIT License 6 votes vote down vote up
public static NumberFormat newCurrencyFormat(Context context, Currency currency, Locale displayLocale) {
        Log.d(TAG, "newCurrencyFormat");
        NumberFormat retVal = NumberFormat.getCurrencyInstance(displayLocale);
        retVal.setCurrency(currency);

        //The default JDK handles situations well when the currency is the default currency for the locale
//        if (currency.equals(Currency.getInstance(displayLocale))) {
//            Log.d(TAG, "Let the JDK handle this");
//            return retVal;
//        }

        //otherwise we need to "fix things up" when displaying a non-native currency
        if (retVal instanceof DecimalFormat) {
            DecimalFormat decimalFormat = (DecimalFormat) retVal;
            String correctedI18NSymbol = getCorrectedInternationalCurrencySymbol(context, currency, displayLocale);
            if (correctedI18NSymbol != null) {
                DecimalFormatSymbols dfs = decimalFormat.getDecimalFormatSymbols(); //this returns a clone of DFS
                dfs.setInternationalCurrencySymbol(correctedI18NSymbol);
                dfs.setCurrencySymbol(correctedI18NSymbol);
                decimalFormat.setDecimalFormatSymbols(dfs);
            }
        }

        return retVal;
    }
 
Example 2
Source File: DecimalFormatSymbolsTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @tests java.text.DecimalFormatSymbols#setInternationalCurrencySymbol(java.lang.String)
 */
public void test_setInternationalCurrencySymbolLjava_lang_String() {
    Locale locale = Locale.CANADA;
    DecimalFormatSymbols dfs = ((DecimalFormat) NumberFormat
            .getCurrencyInstance(locale)).getDecimalFormatSymbols();
    Currency currency = Currency.getInstance("JPY");
    dfs.setInternationalCurrencySymbol(currency.getCurrencyCode());

    assertTrue("Test1: Returned incorrect currency", currency == dfs
            .getCurrency());
    assertEquals("Test1: Returned incorrect currency symbol", currency
            .getSymbol(locale), dfs.getCurrencySymbol());
    assertTrue("Test1: Returned incorrect international currency symbol",
            currency.getCurrencyCode().equals(
                    dfs.getInternationalCurrencySymbol()));

    dfs.setInternationalCurrencySymbol("bogus");
    // RI support this legacy country code
    // assertNotNull("Test2: Returned incorrect currency", dfs.getCurrency());
    assertEquals("Test2: Returned incorrect international currency symbol",
            "bogus", dfs.getInternationalCurrencySymbol());
}
 
Example 3
Source File: DecimalFormatSymbolsTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testSetNulInternationalCurrencySymbol() throws Exception {
    Currency usd = Currency.getInstance("USD");

    DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
    dfs.setCurrency(usd);
    assertEquals(usd, dfs.getCurrency());
    assertEquals("$", dfs.getCurrencySymbol());
    assertEquals("USD", dfs.getInternationalCurrencySymbol());

    // Setting the international currency symbol to null sets the currency to null too,
    // but not the currency symbol.
    dfs.setInternationalCurrencySymbol(null);
    assertEquals(null, dfs.getCurrency());
    assertEquals("$", dfs.getCurrencySymbol());
    assertEquals(null, dfs.getInternationalCurrencySymbol());
}
 
Example 4
Source File: NumberFormatter.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
private DecimalFormatSymbols getICUDecimalSymbols( Locale locale )
{
	DecimalFormatSymbols symbols = new DecimalFormatSymbols( locale );
	com.ibm.icu.text.DecimalFormatSymbols icuSymbols = new com.ibm.icu.text.DecimalFormatSymbols(
			locale );
	symbols.setCurrencySymbol( icuSymbols.getCurrencySymbol( ) );
	symbols.setDecimalSeparator( icuSymbols.getDecimalSeparator( ) );
	symbols.setDigit( icuSymbols.getDigit( ) );
	symbols.setGroupingSeparator( icuSymbols.getGroupingSeparator( ) );
	symbols.setInfinity( icuSymbols.getInfinity( ) );
	symbols.setInternationalCurrencySymbol( icuSymbols
			.getInternationalCurrencySymbol( ) );
	symbols.setMinusSign( icuSymbols.getMinusSign( ) );
	symbols.setMonetaryDecimalSeparator( icuSymbols
			.getMonetaryDecimalSeparator( ) );
	symbols.setNaN( icuSymbols.getNaN( ) );
	symbols.setPatternSeparator( icuSymbols.getPatternSeparator( ) );
	symbols.setPercent( icuSymbols.getPercent( ) );
	symbols.setPerMill( icuSymbols.getPerMill( ) );
	symbols.setZeroDigit( icuSymbols.getZeroDigit( ) );
	return symbols;
}
 
Example 5
Source File: BtcFormat.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set the currency symbol and international code of the underlying {@link
 * java.text.NumberFormat} object to the values of the last two arguments, respectively.
 * This method is invoked in the process of parsing, not formatting.
 * <p>
 * Only invoke this from code synchronized on value of the first argument, and don't
 * forget to put the symbols back otherwise equals(), hashCode() and immutability will
 * break.
 */
private static DecimalFormatSymbols setSymbolAndCode(DecimalFormat numberFormat, String symbol, String code) {
    checkState(Thread.holdsLock(numberFormat));
    DecimalFormatSymbols fs = numberFormat.getDecimalFormatSymbols();
    DecimalFormatSymbols ante = (DecimalFormatSymbols) fs.clone();
    fs.setInternationalCurrencySymbol(code);
    fs.setCurrencySymbol(symbol);
    numberFormat.setDecimalFormatSymbols(fs);
    return ante;
}
 
Example 6
Source File: BtcFormat.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/** Set the currency symbol and international code of the underlying {@link
  * java.text.NumberFormat} object to the values of the last two arguments, respectively.
  * This method is invoked in the process of parsing, not formatting.
  *
  * Only invoke this from code synchronized on value of the first argument, and don't
  * forget to put the symbols back otherwise equals(), hashCode() and immutability will
  * break.  */
private static DecimalFormatSymbols setSymbolAndCode(DecimalFormat numberFormat, String symbol, String code) {
    checkState(Thread.holdsLock(numberFormat));
    DecimalFormatSymbols fs = numberFormat.getDecimalFormatSymbols();
    DecimalFormatSymbols ante = (DecimalFormatSymbols)fs.clone();
    fs.setInternationalCurrencySymbol(code);
    fs.setCurrencySymbol(symbol);
    numberFormat.setDecimalFormatSymbols(fs);
    return ante;
}
 
Example 7
Source File: BtcFormat.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/** Set the currency symbol and international code of the underlying {@link
  * java.text.NumberFormat} object to the values of the last two arguments, respectively.
  * This method is invoked in the process of parsing, not formatting.
  *
  * Only invoke this from code synchronized on value of the first argument, and don't
  * forget to put the symbols back otherwise equals(), hashCode() and immutability will
  * break.  */
private static DecimalFormatSymbols setSymbolAndCode(DecimalFormat numberFormat, String symbol, String code) {
    checkState(Thread.holdsLock(numberFormat));
    DecimalFormatSymbols fs = numberFormat.getDecimalFormatSymbols();
    DecimalFormatSymbols ante = (DecimalFormatSymbols)fs.clone();
    fs.setInternationalCurrencySymbol(code);
    fs.setCurrencySymbol(symbol);
    numberFormat.setDecimalFormatSymbols(fs);
    return ante;
}
 
Example 8
Source File: SakaiDecimalFormatSymbolsProvider.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public DecimalFormatSymbols getInstance(final Locale locale) throws IllegalArgumentException, NullPointerException {
	if (locale == null) {
		throw new NullPointerException("locale:null");
	} else if (!SakaiLocaleServiceProviderUtil.isAvailableLocale(locale)) {
		throw new IllegalArgumentException("locale:" + locale.toString());
	}

	DecimalFormatSymbols symbols = new DecimalFormatSymbols();
	symbols.setDecimalSeparator(
			SakaiLocaleServiceProviderUtil.getChar("DecimalSeparator", locale));
	symbols.setDigit(
			SakaiLocaleServiceProviderUtil.getChar("Digit", locale));
	symbols.setExponentSeparator(
			SakaiLocaleServiceProviderUtil.getString("ExponentSeparator", locale));
	symbols.setGroupingSeparator(
			SakaiLocaleServiceProviderUtil.getChar("GroupingSeparator", locale));
	symbols.setInfinity(
			SakaiLocaleServiceProviderUtil.getString("Infinity", locale));
	symbols.setInternationalCurrencySymbol(
			SakaiLocaleServiceProviderUtil.getString("InternationalCurrencySymbol", locale));
	symbols.setCurrencySymbol(
			SakaiLocaleServiceProviderUtil.getString("CurrencySymbol", locale));
	symbols.setMinusSign(
			SakaiLocaleServiceProviderUtil.getChar("MinusSign", locale));
	symbols.setMonetaryDecimalSeparator(
			SakaiLocaleServiceProviderUtil.getChar("MonetaryDecimalSeparator", locale));
	symbols.setNaN(
			SakaiLocaleServiceProviderUtil.getString("NaN", locale));
	symbols.setPatternSeparator(
			SakaiLocaleServiceProviderUtil.getChar("PatternSeparator", locale));
	symbols.setPercent(
			SakaiLocaleServiceProviderUtil.getChar("Percent", locale));
	symbols.setPerMill(
			SakaiLocaleServiceProviderUtil.getChar("PerMill", locale));
	symbols.setZeroDigit(
			SakaiLocaleServiceProviderUtil.getChar("ZeroDigit", locale));

	return symbols;
}
 
Example 9
Source File: SakaiDecimalFormatSymbolsProvider.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public DecimalFormatSymbols getInstance(final Locale locale) throws IllegalArgumentException, NullPointerException {
	if (locale == null) {
		throw new NullPointerException("locale:null");
	} else if (!SakaiLocaleServiceProviderUtil.isAvailableLocale(locale)) {
		throw new IllegalArgumentException("locale:" + locale.toString());
	}

	DecimalFormatSymbols symbols = new DecimalFormatSymbols();
	symbols.setDecimalSeparator(
			SakaiLocaleServiceProviderUtil.getChar("DecimalSeparator", locale));
	symbols.setDigit(
			SakaiLocaleServiceProviderUtil.getChar("Digit", locale));
	symbols.setExponentSeparator(
			SakaiLocaleServiceProviderUtil.getString("ExponentSeparator", locale));
	symbols.setGroupingSeparator(
			SakaiLocaleServiceProviderUtil.getChar("GroupingSeparator", locale));
	symbols.setInfinity(
			SakaiLocaleServiceProviderUtil.getString("Infinity", locale));
	symbols.setInternationalCurrencySymbol(
			SakaiLocaleServiceProviderUtil.getString("InternationalCurrencySymbol", locale));
	symbols.setCurrencySymbol(
			SakaiLocaleServiceProviderUtil.getString("CurrencySymbol", locale));
	symbols.setMinusSign(
			SakaiLocaleServiceProviderUtil.getChar("MinusSign", locale));
	symbols.setMonetaryDecimalSeparator(
			SakaiLocaleServiceProviderUtil.getChar("MonetaryDecimalSeparator", locale));
	symbols.setNaN(
			SakaiLocaleServiceProviderUtil.getString("NaN", locale));
	symbols.setPatternSeparator(
			SakaiLocaleServiceProviderUtil.getChar("PatternSeparator", locale));
	symbols.setPercent(
			SakaiLocaleServiceProviderUtil.getChar("Percent", locale));
	symbols.setPerMill(
			SakaiLocaleServiceProviderUtil.getChar("PerMill", locale));
	symbols.setZeroDigit(
			SakaiLocaleServiceProviderUtil.getChar("ZeroDigit", locale));

	return symbols;
}