Example usage for java.text NumberFormat setCurrency

List of usage examples for java.text NumberFormat setCurrency

Introduction

In this page you can find the example usage for java.text NumberFormat setCurrency.

Prototype

public void setCurrency(Currency currency) 

Source Link

Document

Sets the currency used by this number format when formatting currency values.

Usage

From source file:org.totschnig.myexpenses.util.Utils.java

static String formatCurrency(BigDecimal amount, Currency currency) {
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    int fractionDigits = currency.getDefaultFractionDigits();
    nf.setCurrency(currency);
    nf.setMinimumFractionDigits(fractionDigits);
    nf.setMaximumFractionDigits(fractionDigits);
    return nf.format(amount);
}

From source file:com.ibuildapp.romanblack.CataloguePlugin.utils.Utils.java

/**
 * compution currency positon/* w  w w .  j a va  2 s . c om*/
 */
public static String currencyToPosition(String currencyStr, float price) {
    try {
        Locale locale = Locale.getDefault();
        Currency currency = Currency.getInstance(currencyStr);
        java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(locale);
        format.setCurrency(currency);

        return format.format(price);
    } catch (Exception e) {
        return "";
    }
}

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

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

    // 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:com.trenako.entities.Money.java

@Override
public String toString() {
    if (this == EMPTY_VALUE) {
        return "-";
    }/*w w w . j  ava 2 s .  c om*/

    BigDecimal v = BigDecimal.valueOf(getValue()).divide(MONEY_VALUE_FACTOR);

    NumberFormat nf = NumberFormat.getCurrencyInstance();

    // the user may have selected a different currency than the default
    Currency currency = Currency.getInstance(getCurrency());
    nf.setCurrency(currency);
    nf.setMinimumFractionDigits(2);

    return nf.format(v);
}

From source file:org.kuali.kra.proposaldevelopment.printing.xmlstream.ProposalDevelopmentXmlStream.java

private String getCurrencyFormat(BigDecimal amount) {
    NumberFormat currencyFormater = new DecimalFormat("###,###,##0");
    currencyFormater.setCurrency(Currency.getInstance(Locale.US));
    return currencyFormater.format(amount.doubleValue());
}

From source file:org.hoteia.qalingo.core.domain.CurrencyReferential.java

public NumberFormat getStandardCurrencyformat() {
    NumberFormat formatter = NumberFormat.getCurrencyInstance();
    if (StringUtils.isNotEmpty(getFormatLocale())) {
        formatter = NumberFormat.getCurrencyInstance(getLocale());
    }/*from   w  w w . ja  v a  2s. c om*/
    Currency currency = Currency.getInstance(getCode());
    formatter.setCurrency(currency);
    return formatter;
}

From source file:ro.expectations.expenses.ui.transactions.TransactionsAdapter.java

private void processCredit(ViewHolder holder, int position) {
    mCursor.moveToPosition(position);//  w  w w  .j  a  v a 2 s.  c o m

    // Set the account
    String toAccount = mCursor.getString(TransactionsFragment.COLUMN_TO_ACCOUNT_TITLE);
    holder.mAccount.setText(toAccount);

    // Set the amount
    double toAmount = NumberUtils
            .roundToTwoPlaces(mCursor.getLong(TransactionsFragment.COLUMN_TO_AMOUNT) / 100.0);
    NumberFormat format = NumberFormat.getCurrencyInstance();
    String toCurrencyCode = mCursor.getString(TransactionsFragment.COLUMN_TO_CURRENCY);
    Currency toCurrency = Currency.getInstance(toCurrencyCode);
    format.setCurrency(toCurrency);
    format.setMaximumFractionDigits(toCurrency.getDefaultFractionDigits());
    holder.mAmount.setText(format.format(toAmount));
    holder.mAmount.setTextColor(ContextCompat.getColor(mContext, R.color.colorGreen700));

    double toBalance = NumberUtils
            .roundToTwoPlaces(mCursor.getLong(TransactionsFragment.COLUMN_TO_BALANCE) / 100.0);
    holder.mRunningBalance.setText(format.format(toBalance));

    // Set the transaction type icon
    holder.mTypeIcon.setImageDrawable(
            DrawableHelper.tint(mContext, R.drawable.ic_call_received_black_24dp, R.color.colorGreen700));
}

From source file:ro.expectations.expenses.ui.transactions.TransactionsAdapter.java

private void processDebit(ViewHolder holder, int position) {
    mCursor.moveToPosition(position);//  w  w w  .  j  ava 2 s  . c  o  m

    // Set the account
    String fromAccount = mCursor.getString(TransactionsFragment.COLUMN_FROM_ACCOUNT_TITLE);
    holder.mAccount.setText(fromAccount);

    // Set the amount
    double fromAmount = NumberUtils
            .roundToTwoPlaces(0 - mCursor.getLong(TransactionsFragment.COLUMN_FROM_AMOUNT) / 100.0);
    NumberFormat format = NumberFormat.getCurrencyInstance();
    String fromCurrencyCode = mCursor.getString(TransactionsFragment.COLUMN_FROM_CURRENCY);
    Currency fromCurrency = Currency.getInstance(fromCurrencyCode);
    format.setCurrency(fromCurrency);
    format.setMaximumFractionDigits(fromCurrency.getDefaultFractionDigits());
    holder.mAmount.setText(format.format(fromAmount));
    holder.mAmount.setTextColor(ContextCompat.getColor(mContext, R.color.colorRed700));

    double fromBalance = NumberUtils
            .roundToTwoPlaces(mCursor.getLong(TransactionsFragment.COLUMN_FROM_BALANCE) / 100.0);
    holder.mRunningBalance.setText(format.format(fromBalance));

    // Set the transaction type icon
    holder.mTypeIcon.setImageDrawable(
            DrawableHelper.tint(mContext, R.drawable.ic_call_made_black_24dp, R.color.colorRed700));
}

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.  j  a va  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:org.totschnig.myexpenses.util.Utils.java

public static String formatCurrency(BigDecimal amount, Currency currency) {
    NumberFormat nf = getNumberFormat();
    int fractionDigits = Money.getFractionDigits(currency);
    nf.setCurrency(currency);
    if (fractionDigits <= 3) {
        nf.setMinimumFractionDigits(fractionDigits);
        nf.setMaximumFractionDigits(fractionDigits);
    } else {/*  w ww .j a  va  2  s  .co  m*/
        nf.setMaximumFractionDigits(fractionDigits);
    }
    return nf.format(amount);
}