Example usage for java.util Currency getDefaultFractionDigits

List of usage examples for java.util Currency getDefaultFractionDigits

Introduction

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

Prototype

public int getDefaultFractionDigits() 

Source Link

Document

Gets the default number of fraction digits used with this currency.

Usage

From source file:Main.java

public static void main(String args[]) {

    Currency curr1 = Currency.getInstance("USD");
    Currency curr2 = Currency.getInstance("JPY");

    System.out.println("USD fraction digits:" + curr1.getDefaultFractionDigits());
    System.out.println("JPY fraction digits:" + curr2.getDefaultFractionDigits());
}

From source file:MainClass.java

public static void main(String args[]) {
    Currency c;

    c = Currency.getInstance(Locale.US);

    System.out.println("Symbol: " + c.getSymbol());
    System.out.println("Default fractional digits: " + c.getDefaultFractionDigits());
}

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);//from   w  ww  . j a v  a 2  s .c  o  m
    nf.setMinimumFractionDigits(fractionDigits);
    nf.setMaximumFractionDigits(fractionDigits);
    return nf.format(amount);
}

From source file:de.ub0r.android.callmeter.ui.prefs.Preferences.java

/**
 * Get the currency symbol from {@link SharedPreferences}.
 * //from   ww  w. ja  v  a 2s. c om
 * @param context
 *            {@link Context}
 * @return currency symbol
 */
public static final String getCurrencySymbol(final Context context) {
    final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
    final String pcs = p.getString(PREFS_CURRENCY_SYMBOL, "");
    if (pcs.length() == 0) {
        if (defaultCurrencySymbol == null) {
            try {
                final Currency cur = Currency.getInstance(Locale.getDefault());
                defaultCurrencySymbol = cur.getSymbol();
                defaultCurrencyDigits = cur.getDefaultFractionDigits();
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "error getting currency", e);
                defaultCurrencySymbol = "$";
                defaultCurrencyDigits = 2;
            }
        }
        return defaultCurrencySymbol;
    } else {
        return pcs;
    }
}

From source file:de.ub0r.android.callmeter.ui.prefs.Preferences.java

/**
 * Get the currency format from {@link SharedPreferences}.
 * /*from w  w w . ja v a 2s.c o  m*/
 * @param context
 *            {@link Context}
 * @return currency format
 */
public static final String getCurrencyFormat(final Context context) {
    final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
    final String pcs = p.getString(PREFS_CURRENCY_FORMAT, "");
    if (pcs.length() == 0) {
        if (defaultCurrencySymbol == null) {
            try {
                final Currency cur = Currency.getInstance(Locale.getDefault());
                defaultCurrencySymbol = cur.getSymbol();
                defaultCurrencyDigits = cur.getDefaultFractionDigits();
            } catch (IllegalArgumentException e) {
                Log.e(TAG, "error getting currency", e);
                defaultCurrencySymbol = "$";
                defaultCurrencyDigits = 2;
            }
        }
        return "%." + defaultCurrencyDigits + "f" + getCurrencySymbol(context);
    } else {
        Log.d(TAG, "custom currency format: " + pcs);
        String c = getCurrencySymbol(context);
        Log.d(TAG, "custom currency symbol: " + c);
        if (c.equals("$")) {
            c = "\\$";
            Log.d(TAG, "custom currency symbol: " + c);
        } else if (c.equals("%")) {
            c = "%%";
            Log.d(TAG, "custom currency symbol: " + c);
        }
        final String ret = pcs.replaceAll("\\$", c).replaceAll("\u20AC", c).replaceAll("\u0440", c);
        Log.d(TAG, "custom currency format: " + ret);
        return ret;
    }
}

From source file:de.javakaffee.web.msm.integration.TestUtils.java

public static void assertDeepEquals(final Object one, final Object another,
        final Map<Object, Object> alreadyChecked) {
    if (one == another) {
        return;//w ww . j  a  va  2  s  .  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 (Set.class.isAssignableFrom(one.getClass())) {
        final Set<?> m1 = (Set<?>) one;
        final Set<?> m2 = (Set<?>) another;
        Assert.assertEquals(m1.size(), m2.size());
        final Iterator<?> iter1 = m1.iterator();
        final Iterator<?> iter2 = m2.iterator();
        while (iter1.hasNext()) {
            assertDeepEquals(iter1.next(), iter2.next());
        }
        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:de.javakaffee.kryoserializers.KryoTest.java

private static void assertDeepEquals(final Object one, final Object another,
        final Map<Object, Object> alreadyChecked) throws Exception {
    if (one == another) {
        return;//from   w ww .  ja va 2  s  .  com
    }
    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 || one instanceof Class<?>) {
        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());
        final Iterator<? extends Map.Entry<?, ?>> iter1 = m1.entrySet().iterator();
        final Iterator<? extends Map.Entry<?, ?>> iter2 = m2.entrySet().iterator();
        while (iter1.hasNext()) {
            Assert.assertTrue(iter2.hasNext());
            assertDeepEquals(iter1.next(), iter2.next(), alreadyChecked);
        }
        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:dk.clanie.money.Money.java

public Money(BigDecimal amount, Currency currency, RoundingMode roundingMode) {
    int scale = currency.getDefaultFractionDigits();
    if (scale < 0)
        scale = 6;/*  ww  w.  j a v  a  2 s. com*/
    this.amount = amount.setScale(scale, roundingMode);
    this.currency = currency;
}

From source file:org.broadleafcommerce.core.pricing.service.workflow.FulfillmentItemPricingActivity.java

/**
 * Returns the unit amount (e.g. .01 for US)
 * @param currency//from  w w w.  java  2  s.  co  m
 * @return
 */
public Money getUnitAmount(Money difference) {
    Currency currency = difference.getCurrency();
    BigDecimal divisor = new BigDecimal(Math.pow(10, currency.getDefaultFractionDigits()));
    BigDecimal unitAmount = new BigDecimal("1").divide(divisor);

    if (difference.lessThan(BigDecimal.ZERO)) {
        unitAmount = unitAmount.negate();
    }
    return new Money(unitAmount, currency);
}

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

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    mCursor.moveToPosition(position);/*from   w  w  w  . j  av a2s.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));
    }
}