Example usage for java.math RoundingMode HALF_EVEN

List of usage examples for java.math RoundingMode HALF_EVEN

Introduction

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

Prototype

RoundingMode HALF_EVEN

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

Click Source Link

Document

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

Usage

From source file:Main.java

public static double round(double num) {
    BigDecimal b = new BigDecimal(num);
    BigDecimal rounded = b.setScale(2, RoundingMode.HALF_EVEN);
    return rounded.doubleValue();

    //return num;
}

From source file:Main.java

public static BigDecimal log10(BigDecimal b) {
    final int NUM_OF_DIGITS = SCALE + 2;
    // need to add one to get the right number of dp
    // and then add one again to get the next number
    // so I can round it correctly.

    MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN);
    // special conditions:
    // log(-x) -> exception
    // log(1) == 0 exactly;
    // log of a number lessthan one = -log(1/x)
    if (b.signum() <= 0) {
        throw new ArithmeticException("log of a negative number! (or zero)");
    } else if (b.compareTo(BigDecimal.ONE) == 0) {
        return BigDecimal.ZERO;
    } else if (b.compareTo(BigDecimal.ONE) < 0) {
        return (log10((BigDecimal.ONE).divide(b, mc))).negate();
    }//from  w  w  w .ja v  a  2  s . co m

    StringBuilder sb = new StringBuilder();
    // number of digits on the left of the decimal point
    int leftDigits = b.precision() - b.scale();

    // so, the first digits of the log10 are:
    sb.append(leftDigits - 1).append(".");

    // this is the algorithm outlined in the webpage
    int n = 0;
    while (n < NUM_OF_DIGITS) {
        b = (b.movePointLeft(leftDigits - 1)).pow(10, mc);
        leftDigits = b.precision() - b.scale();
        sb.append(leftDigits - 1);
        n++;
    }

    BigDecimal ans = new BigDecimal(sb.toString());

    // Round the number to the correct number of decimal places.
    ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN));
    return ans;
}

From source file:Main.java

public static BigDecimal log10(BigDecimal b) {
    final int NUM_OF_DIGITS = SCALE + 2;
    // need to add one to get the right number of dp
    //  and then add one again to get the next number
    //  so I can round it correctly.

    MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN);
    //special conditions:
    // log(-x) -> exception
    // log(1) == 0 exactly;
    // log of a number lessthan one = -log(1/x)
    if (b.signum() <= 0) {
        throw new ArithmeticException("log of a negative number! (or zero)");
    } else if (b.compareTo(BigDecimal.ONE) == 0) {
        return BigDecimal.ZERO;
    } else if (b.compareTo(BigDecimal.ONE) < 0) {
        return (log10((BigDecimal.ONE).divide(b, mc))).negate();
    }/*from  w  ww. jav  a2  s.co m*/

    StringBuilder sb = new StringBuilder();
    //number of digits on the left of the decimal point
    int leftDigits = b.precision() - b.scale();

    //so, the first digits of the log10 are:
    sb.append(leftDigits - 1).append(".");

    //this is the algorithm outlined in the webpage
    int n = 0;
    while (n < NUM_OF_DIGITS) {
        b = (b.movePointLeft(leftDigits - 1)).pow(10, mc);
        leftDigits = b.precision() - b.scale();
        sb.append(leftDigits - 1);
        n++;
    }

    BigDecimal ans = new BigDecimal(sb.toString());

    //Round the number to the correct number of decimal places.
    ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN));
    return ans;
}

From source file:org.niord.core.geojson.GeoJsonUtils.java

/** Rounds the coordinates of the GeoJson object */
public static void roundCoordinates(GeoJsonVo g, int decimals) {
    g.visitCoordinates(c -> {//from w ww  .j a v a 2s.co  m
        c[0] = new BigDecimal(c[0]).setScale(decimals, RoundingMode.HALF_EVEN).doubleValue();
        c[1] = new BigDecimal(c[1]).setScale(decimals, RoundingMode.HALF_EVEN).doubleValue();
    });
}

From source file:org.mayocat.shop.catalog.front.representation.CurrencyRepresentation.java

public CurrencyRepresentation(Currency currency, Locale locale) {
    this.symbol = CURRENCY_FORMATTER.withLocale(locale)
            .print(Money.of(CurrencyUnit.of(currency), BigDecimal.TEN, RoundingMode.HALF_EVEN));

    this.localSymbol = MoneyUtil.getLocalSymbol(currency);
}

From source file:org.opencredo.cloud.storage.samples.quote.QuoteService.java

public void lookupQuote(Message<String> tickerMessage) {
    BigDecimal price = new BigDecimal(new Random().nextDouble() * 100);
    LOG.info("Quote Update... {}: {}", tickerMessage.getPayload(), price.setScale(2, RoundingMode.HALF_EVEN));
}

From source file:org.sparkcommerce.core.offer.service.discount.domain.PromotableOfferUtility.java

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

    OfferDiscountType discountType = offer.getDiscountType();
    Money adjustmentValue;//ww  w .j  a v a 2  s. co m
    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:org.openmrs.module.hospitalcore.HospitalCoreActivator.java

public void started() {
    // TODO Auto-generated method stub
    log.info("Started HOSPITALCORE Module");
    Money.init(Currency.getInstance("INR"), RoundingMode.HALF_EVEN);
}

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  ww  .j  a v a 2s  . co  m*/
    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:org.apache.hive.storage.jdbc.spitter.DecimalIntervalSplitter.java

@Override
public List<MutablePair<String, String>> getIntervals(String lowerBound, String upperBound, int numPartitions,
        TypeInfo typeInfo) {/*from  w  w w .  j  av  a 2  s.  c  o m*/
    List<MutablePair<String, String>> intervals = new ArrayList<>();
    DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) typeInfo;
    int scale = decimalTypeInfo.getScale();
    BigDecimal decimalLower = new BigDecimal(lowerBound);
    BigDecimal decimalUpper = new BigDecimal(upperBound);
    BigDecimal decimalInterval = (decimalUpper.subtract(decimalLower)).divide(new BigDecimal(numPartitions),
            MathContext.DECIMAL64);
    BigDecimal splitDecimalLower, splitDecimalUpper;
    for (int i = 0; i < numPartitions; i++) {
        splitDecimalLower = decimalLower.add(decimalInterval.multiply(new BigDecimal(i))).setScale(scale,
                RoundingMode.HALF_EVEN);
        splitDecimalUpper = decimalLower.add(decimalInterval.multiply(new BigDecimal(i + 1))).setScale(scale,
                RoundingMode.HALF_EVEN);
        if (splitDecimalLower.compareTo(splitDecimalUpper) < 0) {
            intervals.add(new MutablePair<String, String>(splitDecimalLower.toPlainString(),
                    splitDecimalUpper.toPlainString()));
        }
    }
    return intervals;
}