Example usage for java.math MathContext DECIMAL64

List of usage examples for java.math MathContext DECIMAL64

Introduction

In this page you can find the example usage for java.math MathContext DECIMAL64.

Prototype

MathContext DECIMAL64

To view the source code for java.math MathContext DECIMAL64.

Click Source Link

Document

A MathContext object with a precision setting matching the IEEE 754R Decimal64 format, 16 digits, and a rounding mode of RoundingMode#HALF_EVEN HALF_EVEN , the IEEE 754R default.

Usage

From source file:org.mifosplatform.portfolio.savings.service.SavingsAccountWritePlatformServiceJpaRepositoryImpl.java

@Override
public SavingsAccountTransaction acceptSavingsTransfer(final Long accountId, final LocalDate transferDate,
        final Office acceptedInOffice, final Staff fieldOfficer) {
    final SavingsAccount savingsAccount = this.savingAccountAssembler.assembleFrom(accountId);

    final Set<Long> existingTransactionIds = new HashSet<Long>();
    final Set<Long> existingReversedTransactionIds = new HashSet<Long>();
    updateExistingTransactionsDetails(savingsAccount, existingTransactionIds, existingReversedTransactionIds);

    final SavingsAccountTransaction acceptTransferTransaction = SavingsAccountTransaction
            .approveTransfer(savingsAccount, acceptedInOffice, transferDate);
    savingsAccount.getTransactions().add(acceptTransferTransaction);
    savingsAccount.setStatus(SavingsAccountStatusType.ACTIVE.getValue());
    if (fieldOfficer != null) {
        savingsAccount.update(fieldOfficer);
    }//from   ww  w.j  ava2s. c  o m

    final MathContext mc = MathContext.DECIMAL64;
    savingsAccount.calculateInterestUsing(mc, transferDate);

    this.savingsAccountTransactionRepository.save(acceptTransferTransaction);
    this.savingAccountRepository.save(savingsAccount);

    postJournalEntries(savingsAccount, existingTransactionIds, existingReversedTransactionIds);

    return acceptTransferTransaction;
}

From source file:org.mifosplatform.portfolio.savings.service.SavingsAccountWritePlatformServiceJpaRepositoryImpl.java

@Transactional
@Override//ww w  .j  av  a2s .  c om
public CommandProcessingResult waiveCharge(final Long savingsAccountId, final Long savingsAccountChargeId) {

    this.context.authenticatedUser();

    final SavingsAccountCharge savingsAccountCharge = this.savingsAccountChargeRepository
            .findOneWithNotFoundDetection(savingsAccountChargeId, savingsAccountId);

    // Get Savings account from savings charge
    final SavingsAccount account = savingsAccountCharge.savingsAccount();
    this.savingAccountAssembler.assignSavingAccountHelpers(account);

    final Set<Long> existingTransactionIds = new HashSet<Long>();
    final Set<Long> existingReversedTransactionIds = new HashSet<Long>();
    updateExistingTransactionsDetails(account, existingTransactionIds, existingReversedTransactionIds);

    account.waiveCharge(savingsAccountChargeId);

    final MathContext mc = MathContext.DECIMAL64;
    if (account.isBeforeLastPostingPeriod(savingsAccountCharge.getDueLocalDate())) {
        final LocalDate today = DateUtils.getLocalDateOfTenant();
        account.postInterest(mc, today);
    } else {
        final LocalDate today = DateUtils.getLocalDateOfTenant();
        account.calculateInterestUsing(mc, today);
    }

    account.validateAccountBalanceDoesNotBecomeNegative(SavingsApiConstants.waiveChargeTransactionAction);

    this.savingAccountRepository.saveAndFlush(account);

    postJournalEntries(account, existingTransactionIds, existingReversedTransactionIds);

    return new CommandProcessingResultBuilder() //
            .withEntityId(savingsAccountChargeId) //
            .withOfficeId(account.officeId()) //
            .withClientId(account.clientId()) //
            .withGroupId(account.groupId()) //
            .withSavingsId(savingsAccountId) //
            .build();
}

From source file:org.mifosplatform.portfolio.savings.service.SavingsAccountWritePlatformServiceJpaRepositoryImpl.java

@Transactional
private void payCharge(final SavingsAccountCharge savingsAccountCharge, final LocalDate transactionDate,
        final BigDecimal amountPaid, final DateTimeFormatter formatter) {
    // Get Savings account from savings charge
    final SavingsAccount account = savingsAccountCharge.savingsAccount();
    this.savingAccountAssembler.assignSavingAccountHelpers(account);
    final Set<Long> existingTransactionIds = new HashSet<Long>();
    final Set<Long> existingReversedTransactionIds = new HashSet<Long>();
    updateExistingTransactionsDetails(account, existingTransactionIds, existingReversedTransactionIds);
    account.payCharge(savingsAccountCharge, amountPaid, transactionDate, formatter);

    final MathContext mc = MathContext.DECIMAL64;
    if (account.isBeforeLastPostingPeriod(transactionDate)) {
        final LocalDate today = DateUtils.getLocalDateOfTenant();
        account.postInterest(mc, today);
    } else {/*from   w ww  .j av a 2  s  .c  om*/
        final LocalDate today = DateUtils.getLocalDateOfTenant();
        account.calculateInterestUsing(mc, today);
    }

    account.validateAccountBalanceDoesNotBecomeNegative(
            "." + SavingsAccountTransactionType.PAY_CHARGE.getCode());

    this.savingAccountRepository.save(account);

    postJournalEntries(account, existingTransactionIds, existingReversedTransactionIds);
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * Convert an absolute error to a precision.
 *
 * @param x    The value of the variable
 * @param xerr The absolute error in the variable
 * @return The number of valid digits in x.
 * The value is rounded down, and on the pessimistic side for that reason.
 *///from   w ww  .  j  a  v  a2  s  .c  o m
static public int err2prec(BigDecimal x, BigDecimal xerr) {
    return err2prec(xerr.divide(x, MathContext.DECIMAL64).doubleValue());

}

From source file:org.olap4j.xmla.server.impl.Util.java

/**
 * Make a BigDecimal from a double. On JDK 1.5 or later, the BigDecimal
 * precision reflects the precision of the double while with JDK 1.4
 * this is not the case./*from  w w w  .j  a v  a2 s.  com*/
 *
 * @param d the input double
 * @return the BigDecimal
 */
public static BigDecimal makeBigDecimalFromDouble(double d) {
    return new BigDecimal(d, MathContext.DECIMAL64);
}

From source file:org.openbravo.erpCommon.ad_forms.AcctServer.java

private BigDecimal calculateMultipleRatesDifferences(BigDecimal _amount, String currencyIDFrom,
        String currencyIDTo, DocLine line, ConnectionProvider conn) {
    // _amount * ((TrxRate *
    // AccountingRateCurrencyFromCurrencyTo)-AccountingRateCurrencyDocCurrencyTo)
    String conversionDate = DateAcct;
    String strDateFormat = OBPropertiesProvider.getInstance().getOpenbravoProperties()
            .getProperty("dateFormat.java");
    final SimpleDateFormat dateFormat = new SimpleDateFormat(strDateFormat);
    // Calculate accountingRateCurrencyFromCurrencyTo
    BigDecimal accountingRateCurrencyFromCurrencyTo = BigDecimal.ONE;
    if (!currencyIDFrom.equals(currencyIDTo)) {
        ConversionRateDoc conversionRateCurrentDoc = getConversionRateDoc(AD_Table_ID, Record_ID,
                currencyIDFrom, currencyIDTo);
        if (AD_Table_ID.equals(AcctServer.TABLEID_Reconciliation)
                && line instanceof DocLine_FINReconciliation) {
            String transactionID = ((DocLine_FINReconciliation) line).getFinFinAccTransactionId();
            FIN_FinaccTransaction transaction = OBDal.getInstance().get(FIN_FinaccTransaction.class,
                    transactionID);//from   w  ww. j  a  v  a 2s  . c  o m
            conversionDate = dateFormat.format(transaction.getDateAcct());
            conversionRateCurrentDoc = getConversionRateDoc(TABLEID_Transaction, transaction.getId(),
                    currencyIDFrom, currencyIDTo);
        }
        if (conversionRateCurrentDoc != null) {
            accountingRateCurrencyFromCurrencyTo = conversionRateCurrentDoc.getRate();
        } else {
            // I try to find a reversal rate for the doc, if exists i apply it reversal as well
            if (AD_Table_ID.equals(AcctServer.TABLEID_Reconciliation)
                    && line instanceof DocLine_FINReconciliation) {
                String transactionID = ((DocLine_FINReconciliation) line).getFinFinAccTransactionId();
                FIN_FinaccTransaction transaction = OBDal.getInstance().get(FIN_FinaccTransaction.class,
                        transactionID);
                conversionRateCurrentDoc = getConversionRateDoc(TABLEID_Transaction, transaction.getId(),
                        currencyIDTo, currencyIDFrom);
            } else {
                conversionRateCurrentDoc = getConversionRateDoc(AD_Table_ID, Record_ID, currencyIDTo,
                        currencyIDFrom);
            }
            if (conversionRateCurrentDoc != null) {
                accountingRateCurrencyFromCurrencyTo = BigDecimal.ONE.divide(conversionRateCurrentDoc.getRate(),
                        MathContext.DECIMAL64);
            } else {
                accountingRateCurrencyFromCurrencyTo = getConvertionRate(currencyIDFrom, currencyIDTo,
                        conversionDate, "", AD_Client_ID, AD_Org_ID, conn);
            }
        }
    }

    // Calculate accountingRateCurrencyFromCurrencyTo
    BigDecimal accountingRateCurrencyDocCurrencyTo = BigDecimal.ONE;
    if (!C_Currency_ID.equals(currencyIDTo)) {
        ConversionRateDoc conversionRateCurrentDoc = getConversionRateDoc(AD_Table_ID, Record_ID, C_Currency_ID,
                currencyIDTo);
        if (AD_Table_ID.equals(AcctServer.TABLEID_Reconciliation)
                && line instanceof DocLine_FINReconciliation) {
            String transactionID = ((DocLine_FINReconciliation) line).getFinFinAccTransactionId();
            FIN_FinaccTransaction transaction = OBDal.getInstance().get(FIN_FinaccTransaction.class,
                    transactionID);
            conversionDate = dateFormat.format(transaction.getTransactionDate());
            conversionRateCurrentDoc = getConversionRateDoc(TABLEID_Transaction, transaction.getId(),
                    C_Currency_ID, currencyIDTo);
        }
        if (conversionRateCurrentDoc != null) {
            accountingRateCurrencyDocCurrencyTo = conversionRateCurrentDoc.getRate();
        } else {
            // I try to find a reversal rate for the doc, if exists i apply it reversal as well
            if (AD_Table_ID.equals(AcctServer.TABLEID_Reconciliation)
                    && line instanceof DocLine_FINReconciliation) {
                String transactionID = ((DocLine_FINReconciliation) line).getFinFinAccTransactionId();
                FIN_FinaccTransaction transaction = OBDal.getInstance().get(FIN_FinaccTransaction.class,
                        transactionID);
                conversionRateCurrentDoc = getConversionRateDoc(TABLEID_Transaction, transaction.getId(),
                        currencyIDTo, C_Currency_ID);
            } else {
                conversionRateCurrentDoc = getConversionRateDoc(AD_Table_ID, Record_ID, currencyIDTo,
                        C_Currency_ID);
            }
            if (conversionRateCurrentDoc != null) {
                accountingRateCurrencyDocCurrencyTo = BigDecimal.ONE.divide(conversionRateCurrentDoc.getRate(),
                        MathContext.DECIMAL64);
            } else {
                accountingRateCurrencyDocCurrencyTo = getConvertionRate(C_Currency_ID, currencyIDTo,
                        conversionDate, "", AD_Client_ID, AD_Org_ID, conn);
            }
        }
    }
    // Calculate transaction rate
    BigDecimal trxRate = BigDecimal.ONE;
    if (!C_Currency_ID.equals(currencyIDFrom)) {
        if (AD_Table_ID.equals(TABLEID_Payment)) {
            FIN_Payment payment = OBDal.getInstance().get(FIN_Payment.class, Record_ID);
            trxRate = payment.getFinancialTransactionConvertRate();
        } else if (AD_Table_ID.equals(TABLEID_Transaction) || AD_Table_ID.equals(TABLEID_Reconciliation)) {
            String transactionID = Record_ID;
            // When TableID = Reconciliation info is loaded from transaction
            if (AD_Table_ID.equals(AcctServer.TABLEID_Reconciliation)
                    && line instanceof DocLine_FINReconciliation) {
                transactionID = ((DocLine_FINReconciliation) line).getFinFinAccTransactionId();
            }
            FIN_FinaccTransaction transaction = OBDal.getInstance().get(FIN_FinaccTransaction.class,
                    transactionID);
            trxRate = transaction.getForeignConversionRate();
        }
    }
    Currency currencyFrom = OBDal.getInstance().get(Currency.class, currencyIDTo);
    return _amount
            .multiply(trxRate.multiply(accountingRateCurrencyDocCurrencyTo)
                    .subtract(accountingRateCurrencyFromCurrencyTo))
            .setScale(currencyFrom.getStandardPrecision().intValue(), BigDecimal.ROUND_HALF_EVEN);
}

From source file:org.pentaho.di.core.row.ValueDataUtil.java

public static BigDecimal multiplyBigDecimals(BigDecimal a, BigDecimal b, MathContext mc) {
    if (mc == null) {
        mc = MathContext.DECIMAL64;
    }/*from www  .j  a  v  a  2  s  . com*/
    return a.multiply(b, mc);
}

From source file:org.pentaho.di.core.row.ValueDataUtil.java

public static BigDecimal divideBigDecimals(BigDecimal a, BigDecimal b, MathContext mc) {
    if (mc == null) {
        mc = MathContext.DECIMAL64;
    }/*from  w  w w .  ja  va 2  s  . co  m*/
    return a.divide(b, mc);
}

From source file:org.rhq.server.metrics.MetricsServerTest.java

static double divide(double dividend, int divisor) {
    return new BigDecimal(Double.toString(dividend))
            .divide(new BigDecimal(Integer.toString(divisor)), MathContext.DECIMAL64).doubleValue();
}

From source file:org.yccheok.jstock.analysis.ArithmeticOperator.java

private Double division() {
    Object object0 = inputs[0].getValue();
    Object object1 = inputs[1].getValue();

    try {/*w  ww  . ja v  a 2 s  .c  om*/
        BigDecimal d0 = new BigDecimal(object0.toString());
        BigDecimal d1 = new BigDecimal(object1.toString());

        final double d1Value = d1.doubleValue();
        if (d1Value != 0.0) {
            BigDecimal result = d0.divide(d1, MathContext.DECIMAL64);
            return result.doubleValue();
        }
    } catch (NumberFormatException exp) {
        log.error(null, exp);
    }

    return null;
}