Example usage for java.math BigDecimal setScale

List of usage examples for java.math BigDecimal setScale

Introduction

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

Prototype

@Deprecated(since = "9")
public BigDecimal setScale(int newScale, int roundingMode) 

Source Link

Document

Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal 's unscaled value by the appropriate power of ten to maintain its overall value.

Usage

From source file:Main.java

/**
 * Write a {@link BigDecimal} value into XML output.
 *
 * @param value//from   www.  ja  v a2  s  .  co  m
 * value to write
 *
 * @param min
 * minimal value
 *
 * @param max
 * maximal value
 *
 * @param precision
 * number of decimal places
 *
 * @return
 * XML string
 *
 * @throws IllegalArgumentException
 * if a validation error occured
 */
private static String printDecimal(BigDecimal value, BigDecimal min, BigDecimal max, int precision,
        boolean zeroIncluded) {
    if (value == null) {
        throw new IllegalArgumentException("The provided double value NULL is invalid!");
    }
    value = value.setScale(precision, BigDecimal.ROUND_HALF_UP);

    if (min != null && value.compareTo(min) <= 0) {
        if (!zeroIncluded || !BigDecimal.ZERO.equals(value)) {
            throw new IllegalArgumentException(
                    "The provided double value " + value + " is too small (minimum is " + min + ")!");
        }
    }
    if (max != null && value.compareTo(max) >= 0) {
        throw new IllegalArgumentException(
                "The provided double value " + value + " is too high (maximum is " + max + ")!");
    }

    return DatatypeConverter.printDecimal(value);
}

From source file:model.experiments.tuningRuns.CompetitiveAveragingGridSearch.java

/**
 * Round to certain number of decimals//  w  w w .j  a va2  s.  c  o  m
 *
 * @param d
 * @param decimalPlace
 * @return
 */
public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

From source file:NumberUtil.java

/**
 * Returns the BigDecimal value n with exactly
 * 'prec' decimal places.//from w w w  .  j av a  2  s  .  c o  m
 * Zeroes are padded to the right of the decimal
 * point if necessary.
 */
public static BigDecimal format(BigDecimal n, int prec) {
    return n.setScale(prec, BigDecimal.ROUND_HALF_UP);
}

From source file:net.vexelon.bgrates.Utils.java

public static String scaleNumber(BigDecimal number, int n) {
    return number.setScale(n, BigDecimal.ROUND_HALF_UP).toPlainString();
}

From source file:com.gm.machine.util.CommonUtils.java

/**
 * ?????/*from w  ww.ja v  a2 s.  c om*/
 * 
 * @param a
 * @return
 */
public static double getDecimal(double a) {
    BigDecimal bd = new BigDecimal(a);
    bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}

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://from w  w  w.  j av a2  s  . co  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.broadleafcommerce.core.offer.service.discount.domain.PromotableOfferUtility.java

public static Money computeAdjustmentValue(Money currentPriceDetailValue, BigDecimal offerUnitValue,
        OfferHolder offerHolder, PromotionRounding rounding) {
    Offer offer = offerHolder.getOffer();
    BroadleafCurrency currency = offerHolder.getCurrency();

    OfferDiscountType discountType = offer.getDiscountType();
    Money adjustmentValue;//from  w  w w  .ja  va2s  .c  om
    if (currency != null) {
        adjustmentValue = new Money(currency);
    } else {
        adjustmentValue = new Money();
    }

    if (OfferDiscountType.AMOUNT_OFF.equals(discountType)) {
        adjustmentValue = new Money(offerUnitValue, currency);
    }

    if (OfferDiscountType.FIX_PRICE.equals(discountType)) {
        adjustmentValue = currentPriceDetailValue.subtract(new Money(offerUnitValue, currency));
    }

    if (OfferDiscountType.PERCENT_OFF.equals(discountType)) {
        BigDecimal offerValue = currentPriceDetailValue.getAmount()
                .multiply(offerUnitValue.divide(new BigDecimal("100"), 5, RoundingMode.HALF_EVEN));

        if (rounding.isRoundOfferValues()) {
            offerValue = offerValue.setScale(rounding.getRoundingScale(), rounding.getRoundingMode());
        }
        adjustmentValue = new Money(offerValue, currency);
    }

    if (currentPriceDetailValue.lessThan(adjustmentValue)) {
        adjustmentValue = currentPriceDetailValue;
    }
    return adjustmentValue;
}

From source file:com.hotelbeds.hotelapimodel.auto.util.AssignUtils.java

public static BigDecimal getBigDecimalForPercentageTag(BigDecimal amount) {
    if (amount == null) {
        return null;
    }//from  w  ww.j a va2 s.co m
    return amount.setScale(PERCENTAGE_NUMBER_OF_DECIMALS, BigDecimal.ROUND_HALF_EVEN);
}

From source file:com.hotelbeds.hotelapimodel.auto.util.AssignUtils.java

public static BigDecimal getBigDecimalForPriceTag(BigDecimal amount) {
    if (amount == null) {
        return null;
    }/*w  w w  .  j  av  a2s  . co  m*/
    return amount.setScale(PRICE_NUMBER_OF_DECIMALS, BigDecimal.ROUND_HALF_EVEN);
}

From source file:com.buffalokiwi.aerodrome.jet.Utils.java

/**
 * Round something with bankers rounding
 * @param d float to round//  w w  w . j  a  va  2  s.c om
 * @param decimalPlace places to round to
 * @return new float
 */
public static float round(float d, int decimalPlace, RoundingMode mode) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, mode);

    return bd.floatValue();
}