Example usage for java.util Currency getCurrencyCode

List of usage examples for java.util Currency getCurrencyCode

Introduction

In this page you can find the example usage for java.util Currency getCurrencyCode.

Prototype

public String getCurrencyCode() 

Source Link

Document

Gets the ISO 4217 currency code of this currency.

Usage

From source file:com.facebook.InsightsLogger.java

/**
 * Logs a purchase event with Facebook, in the specified amount and with the specified currency.  Additional
 * detail about the purchase can be passed in through the parameters bundle.
 *
 * @param purchaseAmount  Amount of purchase, in the currency specified by the 'currency' parameter. This value
 *                        will be rounded to the thousandths place (e.g., 12.34567 becomes 12.346).
 * @param currency        Currency used to specify the amount.
 * @param parameters      Arbitrary additional information for describing this event.  Should have no more than
 *                        10 entries, and keys should be mostly consistent from one purchase event to the next.
 *//*from   ww w . j  ava  2s  . c om*/
public void logPurchase(BigDecimal purchaseAmount, Currency currency, Bundle parameters) {

    if (purchaseAmount == null) {
        notifyDeveloperError("purchaseAmount cannot be null");
        return;
    } else if (currency == null) {
        notifyDeveloperError("currency cannot be null");
        return;
    }

    if (parameters == null) {
        parameters = new Bundle();
    }
    parameters.putString(EVENT_PARAMETER_CURRENCY, currency.getCurrencyCode());

    logEventNow(EVENT_NAME_LOG_MOBILE_PURCHASE, purchaseAmount.doubleValue(), parameters);
}

From source file:org.apache.cordova.core.Globalization.java

private JSONObject getCurrencyPattern(JSONArray options) throws GlobalizationError {
    JSONObject obj = new JSONObject();
    try {/*from w w  w .  ja v  a 2  s  .  c  o m*/
        //get ISO 4217 currency code
        String code = options.getJSONObject(0).getString(CURRENCYCODE);

        //uses java.text.DecimalFormat to format value
        DecimalFormat fmt = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.getDefault());

        //set currency format
        Currency currency = Currency.getInstance(code);
        fmt.setCurrency(currency);

        //return properties
        obj.put("pattern", fmt.toPattern());
        obj.put("code", currency.getCurrencyCode());
        obj.put("fraction", fmt.getMinimumFractionDigits());
        obj.put("rounding", new Integer(0));
        obj.put("decimal", String.valueOf(fmt.getDecimalFormatSymbols().getDecimalSeparator()));
        obj.put("grouping", String.valueOf(fmt.getDecimalFormatSymbols().getGroupingSeparator()));

        return obj;
    } catch (Exception ge) {
        throw new GlobalizationError(GlobalizationError.FORMATTING_ERROR);
    }
}

From source file:de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderTest.java

private void assertDeepEquals(final Object one, final Object another, final Map<Object, Object> alreadyChecked)
        throws Exception {
    if (one == another) {
        return;/*from  w w  w. j  a  v  a  2s.  c o m*/
    }
    if (one == null && another != null || one != null && another == null) {
        Assert.fail("One of both is null: " + one + ", " + another);
    }
    if (alreadyChecked.containsKey(one)) {
        return;
    }
    alreadyChecked.put(one, another);

    Assert.assertEquals(one.getClass(), another.getClass());
    if (one.getClass().isPrimitive() || one instanceof String || one instanceof Character
            || one instanceof Boolean) {
        Assert.assertEquals(one, another);
        return;
    }

    if (Map.class.isAssignableFrom(one.getClass())) {
        final Map<?, ?> m1 = (Map<?, ?>) one;
        final Map<?, ?> m2 = (Map<?, ?>) another;
        Assert.assertEquals(m1.size(), m2.size());
        for (final Map.Entry<?, ?> entry : m1.entrySet()) {
            assertDeepEquals(entry.getValue(), m2.get(entry.getKey()));
        }
        return;
    }

    if (Number.class.isAssignableFrom(one.getClass())) {
        Assert.assertEquals(((Number) one).longValue(), ((Number) another).longValue());
        return;
    }

    if (one instanceof Currency) {
        // Check that the transient field defaultFractionDigits is initialized correctly (that was issue #34)
        final Currency currency1 = (Currency) one;
        final Currency currency2 = (Currency) another;
        Assert.assertEquals(currency1.getCurrencyCode(), currency2.getCurrencyCode());
        Assert.assertEquals(currency1.getDefaultFractionDigits(), currency2.getDefaultFractionDigits());
    }

    Class<? extends Object> clazz = one.getClass();
    while (clazz != null) {
        assertEqualDeclaredFields(clazz, one, another, alreadyChecked);
        clazz = clazz.getSuperclass();
    }

}

From source file:org.apache.cordova.globalization.Globalization.java

private JSONObject getCurrencyPattern(JSONArray options) throws GlobalizationError {
    JSONObject obj = new JSONObject();
    try {//from www  .  j a v  a  2 s  .  c o  m
        //get ISO 4217 currency code
        String code = options.getJSONObject(0).getString(CURRENCYCODE);

        //uses java.text.DecimalFormat to format value
        DecimalFormat fmt = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.getDefault());

        //set currency format
        Currency currency = Currency.getInstance(code);
        fmt.setCurrency(currency);

        //return properties
        obj.put("pattern", fmt.toPattern());
        obj.put("code", currency.getCurrencyCode());
        obj.put("fraction", fmt.getMinimumFractionDigits());
        obj.put("rounding", Integer.valueOf(0));
        obj.put("decimal", String.valueOf(fmt.getDecimalFormatSymbols().getDecimalSeparator()));
        obj.put("grouping", String.valueOf(fmt.getDecimalFormatSymbols().getGroupingSeparator()));

        return obj;
    } catch (Exception ge) {
        throw new GlobalizationError(GlobalizationError.FORMATTING_ERROR);
    }
}

From source file:no.abmu.lise.util.LiseImportExcelParser.java

private void checkCurrency(String currencyCode) {
    if (StringUtil.isEmpty(currencyCode)) {
        // no currency code we report and do nothing.
        logger.error(/*from   www.j a v  a  2s.c o  m*/
                "CURRENCY HAS NO VALUE in sheet(" + getSheetName() + ") row(" + getCurrentRowNumber() + ")");
        return;
    }

    Currency currency = getCurrencyFromCurrencyCode(currencyCode);
    if (currency == null) {
        String errorMessage = "'" + currencyCode + "' is not a valid currencyCode.";
        logger.error(errorMessage);
        throw new IllegalArgumentException(errorMessage);
    }

    if (!AcceptedCurreny.isCurrencyAccepted(currency)) {
        String errorMessage = "'" + currency.getCurrencyCode() + "' is not an accepted currency.";
        logger.error(errorMessage);
        throw new IllegalArgumentException(errorMessage);
    }
}

From source file:org.rythmengine.utils.S.java

/**
 * Format give data into currency using locale info from the engine specified
 * //ww w . j a v  a  2s  .  co  m
 * <p>The method accept any data type. When <code>null</code> is found then 
 * <code>NullPointerException</code> will be thrown out; if an <code>Number</code>
 * is passed in, it will be type cast to <code>Number</code>; otherwise 
 * a <code>Double.valueOf(data.toString())</code> is used to find out
 * the number</p>
 *
 * @param template
 * @param data
 * @param currencyCode
 * @param locale
 * @return the currency
 */
public static String formatCurrency(ITemplate template, Object data, String currencyCode, Locale locale) {
    if (null == data)
        throw new NullPointerException();
    Number number;
    if (data instanceof Number) {
        number = (Number) data;
    } else {
        number = Double.parseDouble(data.toString());
    }
    if (null == locale)
        locale = I18N.locale(template);
    Currency currency = null == currencyCode ? Currency.getInstance(locale)
            : Currency.getInstance(currencyCode);
    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
    numberFormat.setCurrency(currency);
    numberFormat.setMaximumFractionDigits(currency.getDefaultFractionDigits());
    String s = numberFormat.format(number);
    s = s.replace(currency.getCurrencyCode(), currency.getSymbol(locale));
    return s;
}

From source file:org.totschnig.myexpenses.provider.TransactionDatabase.java

private void insertDefaultAccount(SQLiteDatabase db) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_LABEL, mCtx.getString(R.string.default_account_name));
    initialValues.put(KEY_OPENING_BALANCE, 0);
    initialValues.put(KEY_DESCRIPTION, mCtx.getString(R.string.default_account_description));
    Currency localCurrency = Utils.getLocalCurrency();
    initialValues.put(KEY_CURRENCY, localCurrency.getCurrencyCode());
    initialValues.put(KEY_TYPE, AccountType.CASH.name());
    initialValues.put(KEY_GROUPING, Grouping.NONE.name());
    initialValues.put(KEY_COLOR, Account.DEFAULT_COLOR);
    db.insert(TABLE_ACCOUNTS, null, initialValues);
    Money.ensureFractionDigitsAreCached(localCurrency);
}

From source file:de.javakaffee.kryoserializers.KryoTest.java

@Test(enabled = true)
public void testCurrency() throws Exception {
    final Currency currency = Currency.getInstance("EUR");
    final Currency deserialized = deserialize(serialize(currency), Currency.class);
    assertDeepEquals(deserialized, currency);

    // Check that the transient field defaultFractionDigits is initialized correctly
    Assert.assertEquals(deserialized.getCurrencyCode(), currency.getCurrencyCode());
    Assert.assertEquals(deserialized.getDefaultFractionDigits(), currency.getDefaultFractionDigits());
}

From source file:org.openestate.io.is24_csv.Is24CsvRecord.java

public void setWaehrung(Currency value) {
    this.set(FIELD_WAEHRUNG, (value != null) ? value.getCurrencyCode() : "EUR");
}

From source file:biz.wolschon.fileformats.gnucash.baseclasses.SimpleAccount.java

/**
 * @param date ignores transactions after the given date
 * @param currency the currency the result shall be in (use account-currency if null)
 * @return null if the conversion is not possible
 * @see #getBalance(Date)//w  ww. ja v a2 s . c  om
 */
public FixedPointNumber getBalance(final Date date, final Currency currency) {

    FixedPointNumber retval = getBalance(date);

    if (retval == null) {
        LOGGER.warn("SimpleAccount.getBalance() - " + "error creating balance!");
        return null;
    }

    if (currency == null || retval.equals(new FixedPointNumber())) {
        return retval;
    }

    // is conversion needed?
    if (getCurrencyNameSpace().equals(GnucashAccount.CURRENCYNAMESPACE_CURRENCY)
            && getCurrencyID().equals(currency.getCurrencyCode())) {
        return retval;
    }

    ComplexCurrencyTable currencyTable = getGnucashFile().getCurrencyTable();

    if (currencyTable == null) {
        LOGGER.warn("SimpleAccount.getBalance() - cannot transfer "
                + "to given currency because we have no currency-table!");
        return null;
    }

    if (!currencyTable.convertToBaseCurrency(getCurrencyNameSpace(), retval, getCurrencyID())) {
        LOGGER.warn("SimpleAccount.getBalance() - cannot transfer " + "from our currency '"
                + getCurrencyNameSpace() + "'-'" + getCurrencyID() + "' to the base-currency!");
        return null;
    }

    if (!currencyTable.convertFromBaseCurrency(retval, currency.getCurrencyCode())) {
        LOGGER.warn("SimpleAccount.getBalance() - cannot transfer " + "from base-currenty to given currency '"
                + currency + "'!");
        return null;
    }

    return retval;
}