Example usage for java.math BigDecimal ROUND_DOWN

List of usage examples for java.math BigDecimal ROUND_DOWN

Introduction

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

Prototype

int ROUND_DOWN

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

Click Source Link

Document

Rounding mode to round towards zero.

Usage

From source file:Main.java

/**
 * Compute e^x to a given scale. Break x into its whole and fraction parts
 * and compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * //  w  w w . j av a  2  s.  c  om
 * @param x
 *            the value of x
 * @param scale
 *            the desired scale of the result
 * @return the result value
 */
public static BigDecimal exp(BigDecimal x, int scale) {
    // e^0 = 1
    if (x.signum() == 0) {
        return BigDecimal.valueOf(1);
    }

    // If x is negative, return 1/(e^-x).
    else if (x.signum() == -1) {
        return BigDecimal.valueOf(1).divide(exp(x.negate(), scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    // Compute the whole part of x.
    BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN);

    // If there isn't a whole part, compute and return e^x.
    if (xWhole.signum() == 0) {
        return expTaylor(x, scale);
    }

    // Compute the fraction part of x.
    BigDecimal xFraction = x.subtract(xWhole);

    // z = 1 + fraction/whole
    BigDecimal z = BigDecimal.valueOf(1).add(xFraction.divide(xWhole, scale, BigDecimal.ROUND_HALF_EVEN));

    // t = e^z
    BigDecimal t = expTaylor(z, scale);

    BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE);
    BigDecimal result = BigDecimal.valueOf(1);

    // Compute and return t^whole using intPower().
    // If whole > Long.MAX_VALUE, then first compute products
    // of e^Long.MAX_VALUE.
    while (xWhole.compareTo(maxLong) >= 0) {
        result = result.multiply(intPower(t, Long.MAX_VALUE, scale)).setScale(scale,
                BigDecimal.ROUND_HALF_EVEN);
        xWhole = xWhole.subtract(maxLong);

        Thread.yield();
    }
    return result.multiply(intPower(t, xWhole.longValue(), scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}

From source file:jp.co.acroquest.endosnipe.report.converter.util.calc.BigDecimalCalculator.java

public Object div(Object obj1, Object obj2) {
    BigDecimal decimalData1 = (BigDecimal) obj1;
    BigDecimal decimalData2 = (BigDecimal) obj2;

    return (Object) (decimalData1.divide(decimalData2, BigDecimal.ROUND_DOWN));
}

From source file:com.pavlovmedia.oss.osgi.gelf.lib.GelfMessageSerializer.java

@Override
public void serialize(GelfMessage value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    jgen.writeStartObject();/* w  ww. j  a va 2 s.  c  o  m*/
    jgen.writeStringField("version", value.version);
    jgen.writeStringField("host", value.host);
    jgen.writeStringField("short_message", value.short_message);
    jgen.writeStringField("full_message", value.full_message);

    BigDecimal bd = new BigDecimal(value.timestamp);
    bd = bd.divide(new BigDecimal(1000), BigDecimal.ROUND_DOWN);
    jgen.writeNumberField("timestamp", bd);
    jgen.writeNumberField("level", value.level);
    for (String key : value.additionalFields.keySet()) {
        jgen.writeStringField("_" + key, value.additionalFields.get(key));
    }
    jgen.writeEndObject();
}

From source file:com.netsteadfast.greenstep.bsc.command.WeightBodyCommand.java

private void autoAllocation(BscStructTreeObj treeObj) throws Exception {
    int scale = 2;
    for (VisionVO vision : treeObj.getVisions()) {
        for (int px = 0; px < vision.getPerspectives().size(); px++) {
            PerspectiveVO perspective = vision.getPerspectives().get(px);
            int round = BigDecimal.ROUND_DOWN;
            if ((px + 1) == vision.getPerspectives().size()) {
                round = BigDecimal.ROUND_UP;
            }// w w  w.ja va2s  .c om
            perspective.setWeight(
                    MAX_WEIGHT_VALUE.divide(new BigDecimal(vision.getPerspectives().size()), scale, round));
            for (int ox = 0; ox < perspective.getObjectives().size(); ox++) {
                ObjectiveVO objective = perspective.getObjectives().get(ox);
                round = BigDecimal.ROUND_DOWN;
                if ((ox + 1) == perspective.getObjectives().size()) {
                    round = BigDecimal.ROUND_UP;
                }
                objective.setWeight(MAX_WEIGHT_VALUE.divide(new BigDecimal(perspective.getObjectives().size()),
                        scale, round));
                for (int kx = 0; kx < objective.getKpis().size(); kx++) {
                    KpiVO kpi = objective.getKpis().get(kx);
                    round = BigDecimal.ROUND_DOWN;
                    if ((kx + 1) == objective.getKpis().size()) {
                        round = BigDecimal.ROUND_UP;
                    }
                    kpi.setWeight(
                            MAX_WEIGHT_VALUE.divide(new BigDecimal(objective.getKpis().size()), scale, round));
                }

            }
        }
    }
}

From source file:gemfire.practice.driver.OrderExample.java

private void findOrders(Customer[] customers) {
    log.debug("looking for orders for customer ID " + customers[1].getId());

    for (Order order : orderService.findOrdersByCustomerId(customers[1].getId())) {
        log.debug("********************found order ID " + order.getId() + " "
                + order.getBillingAddress().getStreet() + " " + order.getBillingAddress().getCity() + " "
                + order.getBillingAddress().getCountry());
        for (LineItem lineItem : order.getLineItems()) {
            log.debug("product ID:" + lineItem.getProductId() + " quantity:" + lineItem.getAmount()
                    + " unit price:" + lineItem.getUnitPrice().setScale(2, BigDecimal.ROUND_DOWN)
                    + " total price:" + lineItem.getTotal().setScale(2, BigDecimal.ROUND_DOWN));
        }//from  w w w. j  a  v a  2 s. c  o m
    }
}

From source file:com.trenako.entities.Money.java

private static int intValue(BigDecimal d) {
    return d.setScale(2, BigDecimal.ROUND_DOWN).multiply(MONEY_VALUE_FACTOR).intValue();
}

From source file:hudson.plugins.robot.model.RobotResult.java

private static double roundToDecimals(double value, int decimals) {
    BigDecimal bd = new BigDecimal(Double.toString(value));
    bd = bd.setScale(decimals, BigDecimal.ROUND_DOWN);
    return bd.doubleValue();
}

From source file:Main.java

/**
 * Compute the natural logarithm of x to a given scale, x > 0.
 * Use Newton's algorithm.//w  ww. jav  a  2  s  .  c om
 */
private static BigDecimal lnNewton(BigDecimal x, int scale) {
    int sp1 = scale + 1;
    BigDecimal n = x;
    BigDecimal term;

    // Convergence tolerance = 5*(10^-(scale+1))
    BigDecimal tolerance = BigDecimal.valueOf(5).movePointLeft(sp1);

    // Loop until the approximations converge
    // (two successive approximations are within the tolerance).
    do {

        // e^x
        BigDecimal eToX = exp(x, sp1);

        // (e^x - n)/e^x
        term = eToX.subtract(n).divide(eToX, sp1, BigDecimal.ROUND_DOWN);

        // x - (e^x - n)/e^x
        x = x.subtract(term);

        Thread.yield();
    } while (term.compareTo(tolerance) > 0);

    return x.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}

From source file:com.fengduo.bee.commons.util.StringFormatter.java

public static String float2format(Double d) {
    if (d == null) {
        return ZERO_STR;
    }/*from   w w  w.ja v a 2 s . com*/

    BigDecimal b = new BigDecimal(d);
    float fb = b.setScale(SCALE_TWO, BigDecimal.ROUND_DOWN).floatValue();
    DecimalFormat format = new DecimalFormat(FORMAT);
    return format.format(fb).toString();
}

From source file:ch.algotrader.entity.security.SecurityFamilyImpl.java

@Override
public BigDecimal roundDown(String broker, BigDecimal price) {

    Validate.notNull(price, "Price cannot be null");

    return RoundUtil.roundToNextN(price, getTickSize(broker, price, false), BigDecimal.ROUND_DOWN);
}