Example usage for java.util Currency getInstance

List of usage examples for java.util Currency getInstance

Introduction

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

Prototype

public static Currency getInstance(Locale locale) 

Source Link

Document

Returns the Currency instance for the country of the given locale.

Usage

From source file:org.moqui.impl.context.L10nFacadeImpl.java

@Override
public String formatCurrency(Object amount, String uomId, Integer fractionDigits, Locale locale) {
    if (amount == null)
        return "";
    if (amount instanceof CharSequence) {
        if (((CharSequence) amount).length() == 0) {
            return "";
        } else {/*from   w  w  w. ja v a 2 s .c o  m*/
            amount = parseNumber((String) amount, null);
        }
    }

    if (locale == null)
        locale = getLocale();
    NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
    Currency currency = uomId != null && uomId.length() > 0 ? Currency.getInstance(uomId) : null;
    if (currency != null)
        nf.setCurrency(currency);
    if (fractionDigits == null)
        fractionDigits = currency != null ? currency.getDefaultFractionDigits() : 2;
    nf.setMaximumFractionDigits(fractionDigits);
    nf.setMinimumFractionDigits(fractionDigits);
    return nf.format(amount);
}

From source file:ro.expectations.expenses.ui.accounts.AccountsAdapter.java

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    mCursor.moveToPosition(position);// w  w w  . j a  v a2  s . com

    // Set the row background
    ListHelper.setItemBackground(mContext, holder.itemView, isItemSelected(position),
            holder.mAccountIconBackground, holder.mSelectedIconBackground);

    // Set the icon
    String type = mCursor.getString(mCursor.getColumnIndex(ExpensesContract.Accounts.TYPE));
    AccountType accountType = AccountType.valueOf(type);
    if (accountType == AccountType.CREDIT_CARD || accountType == AccountType.DEBIT_CARD) {
        String issuer = mCursor.getString(mCursor.getColumnIndex(ExpensesContract.Accounts.SUBTYPE));
        CardIssuer cardIssuer;
        if (issuer == null) {
            cardIssuer = CardIssuer.OTHER;
        } else {
            try {
                cardIssuer = CardIssuer.valueOf(issuer);
            } catch (final IllegalArgumentException ex) {
                cardIssuer = CardIssuer.OTHER;
            }
        }
        holder.mAccountIcon
                .setImageDrawable(DrawableHelper.tint(mContext, cardIssuer.iconId, R.color.colorWhite));
    } else if (accountType == AccountType.ELECTRONIC) {
        String paymentType = mCursor.getString(mCursor.getColumnIndex(ExpensesContract.Accounts.SUBTYPE));
        ElectronicPaymentType electronicPaymentType;
        if (paymentType == null) {
            electronicPaymentType = ElectronicPaymentType.OTHER;
        } else {
            try {
                electronicPaymentType = ElectronicPaymentType.valueOf(paymentType);
            } catch (IllegalArgumentException ex) {
                electronicPaymentType = ElectronicPaymentType.OTHER;
            }
        }
        holder.mAccountIcon.setImageDrawable(
                DrawableHelper.tint(mContext, electronicPaymentType.iconId, R.color.colorWhite));
    } else {
        holder.mAccountIcon
                .setImageDrawable(DrawableHelper.tint(mContext, accountType.iconId, R.color.colorWhite));
    }

    // Set the icon background color
    GradientDrawable bgShape = (GradientDrawable) holder.mAccountIconBackground.getBackground();
    bgShape.setColor(0xFF000000 | ContextCompat.getColor(mContext, accountType.colorId));

    // Set the description
    holder.mAccountDescription.setText(accountType.titleId);

    // Set the title
    String title = mCursor.getString(mCursor.getColumnIndex(ExpensesContract.Accounts.TITLE));
    holder.mAccountTitle.setText(title);

    // Set the date
    long now = System.currentTimeMillis();
    long lastTransactionAt = mCursor
            .getLong(mCursor.getColumnIndex(ExpensesContract.Accounts.LAST_TRANSACTION_AT));
    if (lastTransactionAt == 0) {
        lastTransactionAt = mCursor.getLong(mCursor.getColumnIndex(ExpensesContract.Accounts.CREATED_AT));
    }
    holder.mAccountLastTransactionAt
            .setText(DateUtils.getRelativeTimeSpanString(lastTransactionAt, now, DateUtils.DAY_IN_MILLIS));

    // Set the account balance
    double balance = NumberUtils.roundToTwoPlaces(
            mCursor.getLong(mCursor.getColumnIndex(ExpensesContract.Accounts.BALANCE)) / 100.0);
    String currencyCode = mCursor.getString(mCursor.getColumnIndex(ExpensesContract.Accounts.CURRENCY));
    Currency currency = Currency.getInstance(currencyCode);
    NumberFormat format = NumberFormat.getCurrencyInstance();
    format.setCurrency(currency);
    format.setMaximumFractionDigits(currency.getDefaultFractionDigits());
    holder.mAccountBalance.setText(format.format(balance));
    if (balance > 0) {
        holder.mAccountBalance.setTextColor(ContextCompat.getColor(mContext, R.color.colorGreen700));
    } else if (balance < 0) {
        holder.mAccountBalance.setTextColor(ContextCompat.getColor(mContext, R.color.colorRed700));
    }
}

From source file:org.apache.aries.blueprint.itests.Helper.java

private static void checkFoo(Bar bar, Foo foo) throws ParseException {
    assertEquals(5, foo.getA());//from   www  .  j  a v a2 s .  com
    assertEquals(10, foo.getB());
    assertSame(bar, foo.getBar());
    assertEquals(Currency.getInstance("PLN"), foo.getCurrency());
    assertEquals(new SimpleDateFormat("yyyy.MM.dd").parse("2009.04.17"), foo.getDate());

    assertTrue(foo.isInitialized());
    assertFalse(foo.isDestroyed());
}

From source file:no.abmu.lise.domain.HsqlMemHibernate3VanillaTest.java

public void testInvoiceToCustomer() throws Exception {

    Consortium consortium = new Consortium();
    OrganisationUnit customer = new OrganisationUnit();
    Product product = new Product();

    VanillaTester.populateAndPersistObject(session, customer);
    VanillaTester.populateObject(consortium);
    VanillaTester.populateObject(product);
    consortium.addProduct(product);//from  w ww  .  j  av  a 2 s.c o m
    VanillaTester.persistObject(session, consortium);

    ProductCustomerRelation productCustomerRelation = new ProductCustomerRelation(product, customer);
    VanillaTester.populateAndPersistObject(session, productCustomerRelation);

    InvoiceToCustomer invoiceToCustomer = new InvoiceToCustomer();
    invoiceToCustomer.setCustomerInvoiceType(CustomerInvoiceType.SUBSCRIPTION_INVOICE);
    invoiceToCustomer.setProductCustomerRelation(productCustomerRelation);
    invoiceToCustomer.setCurrency(Currency.getInstance("USD"));

    VanillaTester.populatePersistLoadAndTesObject(session, invoiceToCustomer);
}

From source file:org.gnucash.android.ui.report.PieChartFragment.java

/**
 * Manages all actions about displaying the pie chart
 *///from  ww  w.j  a va  2 s.  c o m
private void displayChart() {
    mSelectedValueTextView.setText(R.string.label_select_pie_slice_to_see_details);
    mChart.highlightValues(null);
    mChart.clear();

    PieData pieData = getData();
    if (pieData != null && pieData.getYValCount() != 0) {
        mChartDataPresent = true;
        mChart.setData(mGroupSmallerSlices ? groupSmallerSlices(pieData, getActivity()) : pieData);
        float sum = mChart.getData().getYValueSum();
        String total = getResources().getString(R.string.label_chart_total);
        String currencySymbol = Currency.getInstance(mCurrencyCode).getSymbol(Locale.getDefault());
        mChart.setCenterText(String.format(TOTAL_VALUE_LABEL_PATTERN, total, sum, currencySymbol));
        mChart.animateXY(ANIMATION_DURATION, ANIMATION_DURATION);
    } else {
        mChartDataPresent = false;
        mChart.setCenterText(getResources().getString(R.string.label_chart_no_data));
        mChart.setData(getEmptyData());
    }

    mChart.setTouchEnabled(mChartDataPresent);
    mChart.invalidate();
}

From source file:org.sparkcommerce.admin.server.service.persistence.module.provider.SkuPricingPersistenceProvider.java

@Override
protected Currency getCurrency(ExtractValueRequest extractValueRequest, Property property) {
    SparkCurrency bc = null;//from ww  w.j av a 2  s  . c  o m
    if (extractValueRequest.getEntity() instanceof Product
            && isDefaultSkuProperty(extractValueRequest, property)) {
        Product p = (Product) extractValueRequest.getEntity();
        bc = p.getDefaultSku().getCurrency();
    } else if (extractValueRequest.getEntity() instanceof Sku) {
        Sku s = (Sku) extractValueRequest.getEntity();
        bc = s.getCurrency();
    }

    if (bc == null) {
        SparkRequestContext brc = SparkRequestContext.getSparkRequestContext();
        return brc.getJavaCurrency();
    } else {
        return Currency.getInstance(bc.getCurrencyCode());
    }
}

From source file:org.osframework.contract.date.fincal.definition.CentralBank.java

/**
 * Set currency managed by this central bank.
 * /*from  w ww .  ja  v  a2 s  . c  o  m*/
 * @param currencyCode ISO-4217 code of currency managed by this central bank
 * @throws IllegalArgumentException if currencyCode is not a supported
 *         ISO-4217 code
 */
public void setCurrency(String currencyCode) {
    this.setCurrency(Currency.getInstance(currencyCode));
}

From source file:org.broadleafcommerce.admin.server.service.persistence.module.provider.SkuPricingPersistenceProvider.java

@Override
protected Currency getCurrency(ExtractValueRequest extractValueRequest, Property property) {
    BroadleafCurrency bc = null;/*from   www.  java 2 s . c  o m*/
    if (extractValueRequest.getEntity() instanceof Product
            && isDefaultSkuProperty(extractValueRequest, property)) {
        Product p = (Product) extractValueRequest.getEntity();
        bc = p.getDefaultSku().getCurrency();
    } else if (extractValueRequest.getEntity() instanceof Sku) {
        Sku s = (Sku) extractValueRequest.getEntity();
        bc = s.getCurrency();
    }

    if (bc == null) {
        BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
        return brc.getJavaCurrency();
    } else {
        return Currency.getInstance(bc.getCurrencyCode());
    }
}

From source file:org.gnucash.android.test.ui.TransactionsActivityTest.java

private void validateEditTransactionFields(Transaction transaction) {

    String name = mSolo.getEditText(0).getText().toString();
    assertEquals(transaction.getName(), name);

    String amountString = mSolo.getEditText(1).getText().toString();
    NumberFormat formatter = NumberFormat.getInstance();
    try {//ww  w .  jav a  2 s.com
        amountString = formatter.parse(amountString).toString();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Money amount = new Money(amountString, Currency.getInstance(Locale.getDefault()).getCurrencyCode());
    assertEquals(transaction.getAmount(), amount);

    String description = mSolo.getEditText(2).getText().toString();
    assertEquals(transaction.getDescription(), description);

    String expectedValue = TransactionFormFragment.DATE_FORMATTER.format(transaction.getTimeMillis());
    TextView dateView = (TextView) mSolo.getView(R.id.input_date);
    String actualValue = dateView.getText().toString(); //mSolo.getText(6).getText().toString();
    assertEquals(expectedValue, actualValue);

    expectedValue = TransactionFormFragment.TIME_FORMATTER.format(transaction.getTimeMillis());
    TextView timeView = (TextView) mSolo.getView(R.id.input_time);
    actualValue = timeView.getText().toString();// mSolo.getText(7).getText().toString();
    assertEquals(expectedValue, actualValue);
}

From source file:de.jfachwert.bank.Waehrung.java

/**
 * Ermittelt die Waehrung. Urspruenglich wurde die Default-Currency ueber
 * <pre>/*w  w w  . j  a  va  2  s.  c o m*/
 *     Currency.getInstance(Locale.getDefault())
 * </pre>
 * ermittelt. Dies fuehrte aber auf der Sun zu Problemen, da dort
 * die Currency fuer die Default-Locale folgende Exception hervorrief:
 * <pre>
 * java.lang.IllegalArgumentException
 *     at java.util.Currency.getInstance(Currency.java:384)
 *     at de.jfachwert.bank.Geldbetrag.&lt;clinit&gt;
 *     ...
 * </pre>
 *
 * @return normalerweise die deutsche Currency
 */
private static Currency getDefaultCurrency() {
    Locale[] locales = { Locale.getDefault(), Locale.GERMANY, Locale.GERMAN };
    for (Locale loc : locales) {
        try {
            return Currency.getInstance(loc);
        } catch (IllegalArgumentException iae) {
            LOG.log(Level.INFO,
                    "No currency for locale '" + loc + "' available on this machine - will try next one.", iae);
        }
    }
    return Currency.getAvailableCurrencies().iterator().next();
}