Example usage for java.math RoundingMode DOWN

List of usage examples for java.math RoundingMode DOWN

Introduction

In this page you can find the example usage for java.math RoundingMode DOWN.

Prototype

RoundingMode DOWN

To view the source code for java.math RoundingMode DOWN.

Click Source Link

Document

Rounding mode to round towards zero.

Usage

From source file:org.apache.calcite.runtime.SqlFunctions.java

/** SQL <code>TRUNCATE</code> operator applied to BigDecimal values. */
public static BigDecimal struncate(BigDecimal b0, int b1) {
    return b0.movePointRight(b1).setScale(0, RoundingMode.DOWN).movePointLeft(b1);
}

From source file:net.pms.util.Rational.java

/**
 * Converts this {@link Rational} to an {@code int}. This conversion is
 * analogous to the <i>narrowing primitive conversion</i> from
 * {@code double} to {@code int} as defined in section 5.1.3 of <cite>The
 * Java&trade; Language Specification</cite>: any fractional part of this
 * {@link Rational} will be discarded, and if the resulting "
 * {@link BigInteger}" is too big to fit in an {@code int}, only the
 * low-order 32 bits are returned.//w ww  .  j ava 2  s  .c om
 * <p>
 * Note that this conversion can lose information about the overall
 * magnitude and precision of this {@link Rational} value as well as return
 * a result with the opposite sign.
 *
 * @return This {@link Rational} converted to an {@code int}.
 */
@Override
public int intValue() {
    if (isNaN()) {
        return 0;
    }
    if (isInfinite()) {
        return numerator.signum() > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
    }
    return new BigDecimal(reducedNumerator).divide(new BigDecimal(reducedDenominator), RoundingMode.DOWN)
            .intValue();
}

From source file:net.pms.util.Rational.java

/**
 *
 * Converts this {@link Rational} to a {@code long}. This conversion is
 * analogous to the <i>narrowing primitive conversion</i> from
 * {@code double} to {@code long} as defined in section 5.1.3 of <cite>The
 * Java&trade; Language Specification</cite>: any fractional part of this
 * {@link Rational} will be discarded, and if the resulting "
 * {@link BigInteger}" is too big to fit in a {@code long}, only the
 * low-order 64 bits are returned./*from   ww  w.j a va  2  s.c o m*/
 * <p>
 * Note that this conversion can lose information about the overall
 * magnitude and precision of this {@link Rational} value as well as return
 * a result with the opposite sign.
 *
 * @return This {@link Rational} converted to a {@code long}.
 */
@Override
public long longValue() {
    if (isNaN()) {
        return 0;
    }
    if (isInfinite()) {
        return numerator.signum() > 0 ? Long.MAX_VALUE : Long.MIN_VALUE;
    }
    return new BigDecimal(reducedNumerator).divide(new BigDecimal(reducedDenominator), RoundingMode.DOWN)
            .longValue();
}

From source file:net.pms.util.Rational.java

/**
 * Converts this {@link Rational} to a {@link BigInteger}. This conversion
 * is analogous to the <i>narrowing primitive conversion</i> from
 * {@code double} to {@code long} as defined in section 5.1.3 of <cite>The
 * Java&trade; Language Specification</cite>: any fractional part of this
 * {@link Rational} will be discarded.//from   w  w  w . j  a va2  s.c  o  m
 *
 * @return This {@link Rational} converted to a {@link BigInteger}.
 * @throws ArithmeticException If this is {@code NaN} or infinite.
 */
@Nonnull
public BigInteger bigIntegerValue() {
    if (isNaN()) {
        throw new ArithmeticException("Impossible to express NaN as BigInteger");
    }
    if (isInfinite()) {
        throw new ArithmeticException("Impossible to express infinity as BigInteger");
    }
    return new BigDecimal(reducedNumerator).divide(new BigDecimal(reducedDenominator), RoundingMode.DOWN)
            .toBigInteger();
}

From source file:org.totschnig.myexpenses.activity.ExpenseEdit.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    if (data == null) {
        return;// w ww .  j  a  v  a2 s.c  om
    }
    int id = loader.getId();
    switch (id) {
    case METHODS_CURSOR:
        mMethodsCursor = data;
        View methodContainer = findViewById(R.id.MethodRow);
        if (mMethodsAdapter == null || !data.moveToFirst()) {
            methodContainer.setVisibility(View.GONE);
        } else {
            methodContainer.setVisibility(View.VISIBLE);
            MatrixCursor extras = new MatrixCursor(new String[] { KEY_ROWID, KEY_LABEL, KEY_IS_NUMBERED });
            extras.addRow(new String[] { "0", "- - - -", "0" });
            mMethodsAdapter.swapCursor(new MergeCursor(new Cursor[] { extras, data }));
            if (mSavedInstance) {
                mTransaction.methodId = mMethodId;
            }
            if (mTransaction.methodId != null) {
                while (data.isAfterLast() == false) {
                    if (data.getLong(data.getColumnIndex(KEY_ROWID)) == mTransaction.methodId) {
                        mMethodSpinner.setSelection(data.getPosition() + 1);
                        break;
                    }
                    data.moveToNext();
                }
            } else {
                mMethodSpinner.setSelection(0);
            }
        }
        break;
    case ACCOUNTS_CURSOR:
        mAccountsAdapter.swapCursor(data);
        mAccounts = new Account[data.getCount()];
        if (mSavedInstance) {
            mTransaction.accountId = mAccountId;
            mTransaction.transfer_account = mTransferAccountId;
        }
        data.moveToFirst();
        boolean selectionSet = false;
        String currencyExtra = getIntent().getStringExtra(KEY_CURRENCY);
        while (data.isAfterLast() == false) {
            int position = data.getPosition();
            Account a = Account.fromCacheOrFromCursor(data);
            mAccounts[position] = a;
            if (!selectionSet && (a.currency.getCurrencyCode().equals(currencyExtra)
                    || (currencyExtra == null && a.getId().equals(mTransaction.accountId)))) {
                mAccountSpinner.setSelection(position);
                setAccountLabel(a);
                selectionSet = true;
            }
            data.moveToNext();
        }
        //if the accountId we have been passed does not exist, we select the first entry
        if (mAccountSpinner.getSelectedItemPosition() == android.widget.AdapterView.INVALID_POSITION) {
            mAccountSpinner.setSelection(0);
            mTransaction.accountId = mAccounts[0].getId();
            setAccountLabel(mAccounts[0]);
        }
        if (mOperationType == MyExpenses.TYPE_TRANSFER) {
            mTransferAccountCursor = new FilterCursorWrapper(data);
            int selectedPosition = setTransferAccountFilterMap();
            mTransferAccountsAdapter.swapCursor(mTransferAccountCursor);
            mTransferAccountSpinner.setSelection(selectedPosition);
            mTransaction.transfer_account = mTransferAccountSpinner.getSelectedItemId();
            configureTransferInput();
            if (!mNewInstance && !(mTransaction instanceof Template)) {
                isProcessingLinkedAmountInputs = true;
                mTransferAmountText.setAmount(mTransaction.getTransferAmount().getAmountMajor().abs());
                updateExchangeRates();
                isProcessingLinkedAmountInputs = false;
            }
        } else {
            //the methods cursor is based on the current account,
            //hence it is loaded only after the accounts cursor is loaded
            if (!(mTransaction instanceof SplitPartCategory)) {
                mManager.initLoader(METHODS_CURSOR, null, this);
            }
        }
        mTypeButton.setEnabled(true);
        configureType();
        configureStatusSpinner();
        if (mIsResumed)
            setupListeners();
        break;
    case LAST_EXCHANGE_CURSOR:
        if (data.moveToFirst()) {
            final Currency currency1 = getCurrentAccount().currency;
            final Currency currency2 = Account
                    .getInstanceFromDb(mTransferAccountSpinner.getSelectedItemId()).currency;
            if (currency1.getCurrencyCode().equals(data.getString(0))
                    && currency2.getCurrencyCode().equals(data.getString(1))) {
                BigDecimal amount = new Money(currency1, data.getLong(2)).getAmountMajor();
                BigDecimal transferAmount = new Money(currency2, data.getLong(3)).getAmountMajor();
                BigDecimal exchangeRate = amount.compareTo(nullValue) != 0
                        ? transferAmount.divide(amount, EXCHANGE_RATE_FRACTION_DIGITS, RoundingMode.DOWN)
                        : nullValue;
                if (exchangeRate.compareTo(nullValue) != 0) {
                    mExchangeRate1Text.setAmount(exchangeRate);
                }
            }
        }
    }
}

From source file:org.totschnig.myexpenses.activity.ExpenseEdit.java

private void updateExchangeRates() {
    BigDecimal amount = validateAmountInput(mAmountText, false);
    BigDecimal transferAmount = validateAmountInput(mTransferAmountText, false);
    BigDecimal exchangeRate = (amount != null && transferAmount != null && amount.compareTo(nullValue) != 0)
            ? transferAmount.divide(amount, EXCHANGE_RATE_FRACTION_DIGITS, RoundingMode.DOWN)
            : nullValue;//w  ww.j a va  2s .c o  m
    BigDecimal inverseExchangeRate = exchangeRate.compareTo(nullValue) != 0
            ? new BigDecimal(1).divide(exchangeRate, EXCHANGE_RATE_FRACTION_DIGITS, RoundingMode.DOWN)
            : nullValue;
    mExchangeRate1Text.setAmount(exchangeRate);
    mExchangeRate2Text.setAmount(inverseExchangeRate);
}

From source file:atlas.kingj.roi.FrmMain.java

public static String getSignificant(double value, int sigFigs) {
    MathContext mc = new MathContext(sigFigs, RoundingMode.DOWN);
    BigDecimal bigDecimal = new BigDecimal(value, mc);
    return bigDecimal.toPlainString();
}