Example usage for java.math BigDecimal divide

List of usage examples for java.math BigDecimal divide

Introduction

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

Prototype

public BigDecimal divide(BigDecimal divisor, MathContext mc) 

Source Link

Document

Returns a BigDecimal whose value is (this / divisor) , with rounding according to the context settings.

Usage

From source file:com.idylwood.utils.MathUtils.java

private final static double varianceSlow(final double[] data) {
    BigDecimal mean = new BigDecimal(0);
    for (double x : data)
        mean = mean.add(new BigDecimal(x), MathContext.UNLIMITED);
    mean = mean.divide(new BigDecimal(data.length), MathContext.UNLIMITED);
    //mean = new BigDecimal(mean(data));
    BigDecimal ret = new BigDecimal(0);
    for (double x : data) {
        //BigDecimal summand = ret.add(new BigDecimal(x),MathContext.UNLIMITED);
        BigDecimal summand = new BigDecimal(x).subtract(mean, MathContext.UNLIMITED);
        ret = ret.add(summand.pow(2));//from   w  w w. j  a va2s  .com
    }
    ret = ret.divide(new BigDecimal(data.length - 1), MathContext.DECIMAL128);
    return ret.doubleValue();
}

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();// ww w .ja 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:org.datalorax.populace.core.populate.mutator.change.ChangeBigDecimalMutator.java

@Override
public Object mutate(final Type type, final Object currentValue, final Object parent,
        final PopulatorContext config) {
    Validate.isTrue(type.equals(BigDecimal.class), "BigDecimal type expected");
    if (currentValue == null) {
        return null;
    }/*w w  w.  j  a  v a 2s.  co  m*/

    final BigDecimal bigDecimal = (BigDecimal) currentValue;
    return bigDecimal.divide(DIVISOR, 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.haulmont.timesheets.listener.TimeEntryListener.java

protected void setTimeInHours(TimeEntry entity) {
    BigDecimal minutes = BigDecimal.valueOf(entity.getTimeInMinutes()).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    entity.setTimeInHours(minutes.divide(MINUTES_IN_HOUR, BigDecimal.ROUND_HALF_DOWN));
}

From source file:com.mitchellbosecke.pebble.spring.PebbleView.java

@Override
protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    response.setCharacterEncoding(this.characterEncoding);

    long startNanos = System.nanoTime();

    PebbleTemplate template = this.engine.getTemplate(this.templateName);

    // Add beans context
    model.put(BEANS_VARIABLE_NAME, new Beans(this.getApplicationContext()));

    // Add request & response
    model.put(REQUEST_VARIABLE_NAME, request);
    model.put(RESPONSE_VARIABLE_NAME, response);

    // Add session
    model.put(SESSION_VARIABLE_NAME, request.getSession(false));

    // Locale//w w  w.  ja va2  s.co  m
    Locale locale = RequestContextUtils.getLocale(request);

    final Writer writer = response.getWriter();
    try {
        template.evaluate(writer, model, locale);
    } finally {
        writer.flush();
    }

    if (TIMER_LOGGER.isDebugEnabled()) {
        long endNanos = System.nanoTime();

        BigDecimal elapsed = BigDecimal.valueOf(endNanos - startNanos);
        BigDecimal elapsedMs = elapsed.divide(BigDecimal.valueOf(NANOS_IN_SECOND), RoundingMode.HALF_UP);
        TIMER_LOGGER.debug("Pebble template \"{}\" with locale {} processed in {} nanoseconds (approx. {}ms)",
                new Object[] { this.templateName, locale, elapsed, elapsedMs });
    }
}

From source file:org.openvpms.component.system.common.jxpath.BigDecimalOperationDivide.java

@Override
public Object computeValue(EvalContext context) {
    BigDecimal l = TypeConversionUtil.bigDecimalValue(args[0].computeValue(context));
    BigDecimal r = TypeConversionUtil.bigDecimalValue(args[1].computeValue(context));
    return l.divide(r, MathContext.DECIMAL128);
}

From source file:com.qcadoo.mes.cmmsMachineParts.states.MaintenanceEventStateValidationService.java

private Integer calculatePossibleDeviation(Integer progressTime, BigDecimal possibleDeviationPercent) {
    BigDecimal percent = possibleDeviationPercent.divide(new BigDecimal(100), numberService.getMathContext());
    BigDecimal possibleDeviation = percent.multiply(new BigDecimal(progressTime));
    return possibleDeviation.intValue();
}

From source file:org.apache.fineract.portfolio.loanaccount.domain.LoanCharge.java

public static BigDecimal percentageOf(final BigDecimal value, final BigDecimal percentage) {

    BigDecimal percentageOf = BigDecimal.ZERO;

    if (isGreaterThanZero(value)) {
        final MathContext mc = new MathContext(8, MoneyHelper.getRoundingMode());
        final BigDecimal multiplicand = percentage.divide(BigDecimal.valueOf(100l), mc);
        percentageOf = value.multiply(multiplicand, mc);
    }/*from   ww  w. ja  v  a  2 s.c  om*/
    return percentageOf;
}

From source file:org.bhave.sweeper.impl.DoubleSequenceSweep.java

@Override
public int size() {
    BigDecimal diff = to.subtract(from).abs();

    int size = diff.divide(step.abs(), RoundingMode.FLOOR).intValue();

    return (!(to.compareTo(BigDecimal.ZERO) < 0 && from.compareTo(BigDecimal.ZERO) > 0
            || to.compareTo(BigDecimal.ZERO) > 0 && from.compareTo(BigDecimal.ZERO) < 0)) ? (size + 1) : size;
}