Example usage for java.math BigDecimal ROUND_HALF_UP

List of usage examples for java.math BigDecimal ROUND_HALF_UP

Introduction

In this page you can find the example usage for java.math BigDecimal ROUND_HALF_UP.

Prototype

int ROUND_HALF_UP

To view the source code for java.math BigDecimal ROUND_HALF_UP.

Click Source Link

Document

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

Usage

From source file:client.Pi.java

/**
 * Compute the value of pi to the specified number of 
 * digits after the decimal point.  The value is 
 * computed using Machin's formula:/*from   w w  w. j  a v a2s. c o  m*/
 *
 *          pi/4 = 4*arctan(1/5) - arctan(1/239)
 *
 * and a power series expansion of arctan(x) to 
 * sufficient precision.
 */
public static BigDecimal computePi(int digits) {
    int scale = digits + 5;
    BigDecimal arctan1_5 = arctan(5, scale);
    BigDecimal arctan1_239 = arctan(239, scale);
    BigDecimal pi = arctan1_5.multiply(FOUR).subtract(arctan1_239).multiply(FOUR);
    return pi.setScale(digits, BigDecimal.ROUND_HALF_UP);
}

From source file:org.bankinterface.util.Utils.java

/**
 * ??,??,,??.//w  w  w .ja  v  a2s.c om
 * 
 * @param amount
 * @return
 */
public static String amountToYuan(BigDecimal amount) {
    if (amount == null) {
        throw new IllegalArgumentException();
    }
    return amount.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
}

From source file:org.apache.ojb.ejb.ArticleVO.java

public void setPrice(BigDecimal price) {
    if (price != null)
        price.setScale(2, BigDecimal.ROUND_HALF_UP);
    this.price = price;
}

From source file:com.qtplaf.library.util.NumberUtils.java

/**
 * Round a number (in mode that most of us were taught in grade school).
 * //from  w w  w .j av a  2  s.  com
 * @param value The value to round.
 * @param decimals The number of decimal places.
 * @return The rounded value.
 */
public static double round(double value, int decimals) {
    double p = java.lang.Math.pow(10, decimals);
    double v = value * p;
    long l = java.lang.Math.round(v);
    double r = l / p;
    // Ensure exact decimals because sometimes floating point operations yield numbers like 0.49999999999 for 0.5
    return new BigDecimal(r).setScale(decimals, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:ext.paycenter.PayService.java

/**
 * ?email//from  w w w. j a v  a  2  s .  co m
 * 
 * @param email 
 * @param toEmail ?
 * @param payment ????
 * @param serveduration ?
 * @param isIgnoreLocked ??    true
 * @return
 */
public static TransferResult transferByEmail(String email, String toEmail, BigDecimal payment,
        Long serveduration, boolean isIgnoreLocked) {
    if (StringUtils.isBlank(email) || StringUtils.isBlank(toEmail) || null == payment
            || null == serveduration) {
        throw new IllegalArgumentException("??email = " + email + ", toEmail = " + toEmail
                + ", payment = " + payment + ", serveduration = " + serveduration);
    }

    String paymentString = payment.setScale(2, BigDecimal.ROUND_HALF_UP).toString();

    PCResult<Void> result = PCClient.transferByEmail(email, toEmail, paymentString,
            String.valueOf(serveduration), isIgnoreLocked);
    if (result.balanceNotEnough()) {
        return TransferResult.BALANCE_NOT_ENOUGH;
    } else if (result.isSuccess()) {
        return TransferResult.SUCCESS;
    } else if (result.noMatchData()) {
        return TransferResult.INVALID_TOKEN;
    } else if (result.accountLocked()) {
        return TransferResult.ACCOUNT_LOCKED;
    } else {
        LOGGER.error("call transferByEmail error result = " + result);
    }

    return TransferResult.UNKNOWN_ERROR;
}

From source file:ch.algotrader.vo.client.TransactionVOProducer.java

@Override
public TransactionVO convert(final Transaction entity) {

    Validate.notNull(entity, "Transaction is null");

    TransactionVO vo = new TransactionVO();

    vo.setId(entity.getId());//from   www.  j  av a2s .  c om
    vo.setDateTime(entity.getDateTime());
    vo.setQuantity(entity.getQuantity());
    vo.setType(entity.getType());
    // No conversion for target.strategy (can't convert source.getStrategy():Strategy to String)
    // No conversion for target.account (can't convert source.getAccount():ch.algotrader.entity.Account to String)
    vo.setCurrency(entity.getCurrency());
    vo.setPrice(entity.getPrice());

    Security security = entity.getSecurity();
    if (security != null) {
        vo.setName(security.toString());

        int scale = security.getSecurityFamily().getScale();
        vo.setPrice(entity.getPrice().setScale(scale, BigDecimal.ROUND_HALF_UP));
    } else {
        vo.setPrice(
                entity.getPrice().setScale(this.commonConfig.getPortfolioDigits(), BigDecimal.ROUND_HALF_UP));
    }

    vo.setStrategy(entity.getStrategy().toString());
    vo.setValue(entity.getNetValue());
    vo.setTotalCharges(entity.getTotalCharges());

    Account account = entity.getAccount();
    if (account != null) {
        vo.setAccount(account.toString());
    }

    return vo;
}

From source file:com.willetinc.hadoop.mapreduce.dynamodb.BigDecimalSplitter.java

/**
 * Divide numerator by denominator. If impossible in exact mode, use
 * rounding.//w  w  w  . java2s  .co  m
 */
protected static BigDecimal tryDivide(BigDecimal numerator, BigDecimal denominator) {
    try {
        return numerator.divide(denominator);
    } catch (ArithmeticException ae) {
        return numerator.divide(denominator, BigDecimal.ROUND_HALF_UP);
    }
}

From source file:org.efaps.esjp.assets.LifecycleCostAbstract_Base.java

protected void createCost(final Parameter _parameter) throws EFapsException {
    // Sales-Configuration
    final Instance baseCurrInst = Currency.getBaseCurrency();

    final Instance rateCurrInst = _parameter.getParameterValue("rateCurrencyLink") == null ? baseCurrInst
            : Instance.get(CIERP.Currency.getType(), _parameter.getParameterValue("rateCurrencyLink"));

    final Object[] rateObj = new BigDecimal[] { BigDecimal.ONE, BigDecimal.ONE };
    final BigDecimal rate = ((BigDecimal) rateObj[0]).divide((BigDecimal) rateObj[1], 12,
            BigDecimal.ROUND_HALF_UP);

    final DecimalFormat format = getFormatInstance();
    final Insert insert = new Insert(getType4CostCreate(_parameter));
    insert.add(CIAssets.LifecycleCostAbstract.AssetLinkAbstract, _parameter.getInstance().getId());
    insert.add(CIAssets.LifecycleCostAbstract.RateCurrencyLink, rateCurrInst.getId());
    insert.add(CIAssets.LifecycleCostAbstract.CurrencyLink, baseCurrInst.getId());
    insert.add(CIAssets.LifecycleCostAbstract.Rate, rateObj);

    final String date = _parameter
            .getParameterValue(getFieldName4Attribute(_parameter, CIAssets.LifecycleCostAbstract.Date.name));
    if (date != null) {
        insert.add(CIAssets.LifecycleCostAbstract.Date, date);
    }//from  ww w.jav  a 2 s.co  m

    final String rateAmountStr = _parameter.getParameterValue(
            getFieldName4Attribute(_parameter, CIAssets.LifecycleCostAbstract.RateAmount.name));
    if (rateAmountStr != null) {
        try {
            final BigDecimal rateAmount = (BigDecimal) format.parse(rateAmountStr);
            insert.add(CIAssets.LifecycleCostAbstract.RateAmount, rateAmount);
            insert.add(CIAssets.LifecycleCostAbstract.Amount,
                    rateAmount.setScale(8, BigDecimal.ROUND_HALF_UP).divide(rate, BigDecimal.ROUND_HALF_UP));
        } catch (final ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    insert.execute();
}

From source file:engine.Pi.java

/**
     * Compute the value of pi to the specified number of 
     * digits after the decimal point.  The value is 
     * computed using Machin's formula:/*  w  w w  .  j  a v a 2 s  .  c  o m*/
     *
     *          pi/4 = 4*arctan(1/5) - arctan(1/239)
     *
     * and a power series expansion of arctan(x) to 
     * sufficient precision.
     */
    public static BigDecimal computePi(int digits) {
        int scale = digits + 5;
        BigDecimal arctan1_5 = arctan(5, scale);
        BigDecimal arctan1_239 = arctan(239, scale);
        BigDecimal pi = arctan1_5.multiply(FOUR).subtract(arctan1_239).multiply(FOUR);
        return pi.setScale(digits, BigDecimal.ROUND_HALF_UP);
    }

From source file:org.kalypso.model.wspm.tuhh.ui.utils.GuessStationPatternReplacer.java

public static BigDecimal findStation(final String searchString, final GuessStationContext[] searchContexts,
        final Pattern[] searchPatterns) {
    for (int i = 0; i < searchPatterns.length; i++) {
        final Matcher matcher = searchPatterns[i].matcher(searchString);
        if (matcher.matches()) {
            final String stationString = matcher.group(1);

            final BigDecimal station = searchContexts[i].parseStation(stationString);
            if (station == null)
                return null;

            // Set scale to 1, as it is in the database, else we get problems to compare
            // with existing cross sections
            return station.setScale(1, BigDecimal.ROUND_HALF_UP);
        }/*from  w  ww  .  j  a  v  a2s  .c  o m*/
    }

    return null;
}