Example usage for java.math RoundingMode HALF_UP

List of usage examples for java.math RoundingMode HALF_UP

Introduction

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

Prototype

RoundingMode HALF_UP

To view the source code for java.math RoundingMode 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:de.micromata.genome.util.types.Converter.java

/**
 * Creates the amount.//from w  w  w .  j a  v  a 2  s.  c o m
 *
 * @param amount the amount
 * @return the big decimal
 */
public static BigDecimal createAmount(double amount) {
    BigDecimal bd = new BigDecimal(amount);
    bd = bd.setScale(2, RoundingMode.HALF_UP);
    return bd;
}

From source file:op.care.prescription.DlgOnDemand.java

public void initDialog() {
    fillComboBoxes();//  w  w w  .  jav  a2s .  c  o  m

    ignoreEvent = true;

    lblCheckResultAfter.setText(SYSTools.xx("nursingrecords.prescription.dlgOnDemand.outcomeCheck") + ":");

    rbActive.setText(SYSTools.xx("nursingrecords.prescription.dlgOnDemand.rbActive"));

    txtMed.setText("");

    cmbMed.setRenderer(TradeFormTools.getRenderer(TradeFormTools.LONG));

    cmbMed.setModel(new DefaultComboBoxModel());
    cmbDocON.setSelectedItem(prescription.getDocON());
    cmbHospitalON.setSelectedItem(prescription.getHospitalON());
    txtBemerkung.setText(SYSTools.catchNull(prescription.getText()));

    if (prescription.hasMed()) {
        cmbMed.setModel(new DefaultComboBoxModel(new TradeForm[] { prescription.getTradeForm() }));
    }

    cmbIntervention.setModel(
            new DefaultComboBoxModel(InterventionTools.findBy(InterventionTools.TYPE_PRESCRIPTION).toArray()));
    cmbIntervention.setRenderer(InterventionTools.getRenderer());
    cmbIntervention.setEnabled(cmbMed.getModel().getSize() == 0);
    txtIntervention.setEnabled(cmbIntervention.isEnabled());
    cmbIntervention.setSelectedItem(prescription.getIntervention());

    cmbCheckAfter.setSelectedItem(schedule.getCheckAfterHours());

    cmbSit.setRenderer(SituationsTools.getSituationenRenderer());
    cmbSit.setModel(new DefaultComboBoxModel(new Situations[] { prescription.getSituation() }));
    txtSit.setText("");

    txtMaxTimes.setText(NumberFormat.getNumberInstance().format(schedule.getMaxAnzahl()));
    txtEDosis.setText(schedule.getMaxEDosis().setScale(2, RoundingMode.HALF_UP).toString());

    pnlCommonTags = new PnlCommonTags(prescription.getCommontags(), true, 3);
    jPanel1.add(new JScrollPane(pnlCommonTags), CC.xyw(1, 11, 5));

    ignoreEvent = false;
    txtMed.requestFocus();

}

From source file:org.openbravo.costing.AverageCostAdjustment.java

@Override
protected BigDecimal getOutgoingBackdatedTrxAdjAmt(CostAdjustmentLine costAdjLine) {
    // Calculate the average cost on the transaction's movement date and adjust the cost if needed.
    MaterialTransaction trx = costAdjLine.getInventoryTransaction();
    Costing costing = getAvgCostOnMovementDate(trx, getCostDimensions(), getCostOrg(), areBackdatedTrxFixed);

    if (costing == null) {
        // In case the backdated transaction is on a date where the stock was not initialized there
        // isn't any costing entry related to an inventory transaction which results in a null
        // costing.
        // Try again with average algorithm getProductCost method using the movement date as
        // parameter.
        costing = AverageAlgorithm.getProductCost(trx.getMovementDate(), trx.getProduct(), getCostDimensions(),
                getCostOrg());/*from  www. j a v  a  2  s .  c o  m*/
    }

    if (costing == null) {
        String errorMessage = OBMessageUtils.parseTranslation("@NoAvgCostDefined@ @Organization@: "
                + getCostOrg().getName() + ", @Product@: " + trx.getProduct().getName() + ", @Date@: "
                + OBDateUtils.formatDate(trx.getMovementDate()));
        throw new OBException(errorMessage);
    }
    BigDecimal cost = costing.getCost();
    Currency costCurrency = getCostCurrency();
    if (costing.getCurrency() != costCurrency) {
        cost = FinancialUtils.getConvertedAmount(costing.getCost(), costing.getCurrency(), costCurrency,
                trx.getTransactionProcessDate(), getCostOrg(), FinancialUtils.PRECISION_COSTING);
    }
    BigDecimal expectedCostAmt = trx.getMovementQuantity().abs().multiply(cost).setScale(stdCurPrecission,
            RoundingMode.HALF_UP);
    BigDecimal currentCost = trx.getTransactionCost();
    return expectedCostAmt.subtract(currentCost);
}

From source file:CentralLimitTheorem.CLT.java

public void ModelDynamics(int size, double prob) {
    sampleSize = size;/*from   ww  w. j a  v  a2  s  .  c o  m*/
    probability = prob;
    double m = sampleSize * probability;
    double variance_2 = sampleSize * probability * (1. - probability);
    for (int i = 0; i <= 1000; i++) {
        double x = (double) i * ((double) sampleSize / 1000.);
        double exponent = Math.pow(x - m, 2.0) / (2 * variance_2);
        double scale = Math.exp((-1.) * exponent);
        double coeff = Math.sqrt(variance_2 * 2. * Math.PI);
        BigDecimal coeffBD = new BigDecimal(coeff);
        BigDecimal scaleBD = new BigDecimal(scale);
        prob_x[i] = scaleBD.divide(coeffBD, 40, RoundingMode.HALF_UP);

    }

    for (int i = 0; i <= sampleSize; i++) {
        BigInteger ntF = factorial(sampleSize);
        BigInteger denom = factorial(i).multiply(factorial(sampleSize - i));
        BigDecimal ntFBD = new BigDecimal(ntF);
        BigDecimal denomBD = new BigDecimal(denom);
        BigDecimal quotient = ntFBD.divide(denomBD, 40, RoundingMode.HALF_UP);
        BigDecimal restBD = BigDecimal.valueOf(
                Math.pow(probability, (double) i) * Math.pow(1. - probability, (double) sampleSize - i));
        prob_k[i] = quotient.multiply(restBD);
        System.out.format("[%d]\t%.20f\n", i, prob_k[i]);
    }

}

From source file:org.openbravo.costing.AverageCostAdjustment.java

@Override
protected BigDecimal getDefaultCostDifference(TrxType calTrxType, CostAdjustmentLine costAdjLine) {
    MaterialTransaction trx = costAdjLine.getInventoryTransaction();
    Costing costing = getAvgCostOnMovementDate(trx, getCostDimensions(), getCostOrg(), areBackdatedTrxFixed);
    if (costing == null) {
        // In case the backdated transaction is on a date where the stock was not initialized there
        // isn't any costing entry related to an inventory transaction which results in a null
        // costing.
        // Try again with average algorithm getProductCost method using the movement date as
        // parameter.
        costing = AverageAlgorithm.getProductCost(trx.getMovementDate(), trx.getProduct(), getCostDimensions(),
                getCostOrg());//  w  w w . j a  v a2s .c  o m
    }
    if (costing != null) {
        BigDecimal defaultCost = costing.getCost();
        Currency costCurrency = getCostCurrency();
        if (costing.getCurrency() != costCurrency) {
            defaultCost = FinancialUtils.getConvertedAmount(costing.getCost(), costing.getCurrency(),
                    costCurrency, trx.getTransactionProcessDate(), getCostOrg(),
                    FinancialUtils.PRECISION_COSTING);
        }
        BigDecimal trxCalculatedCost = CostAdjustmentUtils.getTrxCost(trx, true, getCostCurrency());
        defaultCost = trx.getMovementQuantity().abs().multiply(defaultCost).setScale(stdCurPrecission,
                RoundingMode.HALF_UP);
        return defaultCost.subtract(trxCalculatedCost);
    }
    return super.getDefaultCostDifference(calTrxType, costAdjLine);
}

From source file:jp.furplag.util.commons.NumberUtilsTest.java

/**
 * {@link jp.furplag.util.commons.NumberUtils#divide(java.lang.Object, java.lang.Number, java.lang.Number, java.math.RoundingMode, java.lang.Class)}.
 *///from w w w .j  a v  a2s . co  m
@SuppressWarnings("unchecked")
@Test
public void testDivideTNumberNumberRoundingModeClassOfT() {
    assertEquals("null", null, divide(null, null, null, null, null));
    assertEquals("null", null, divide(10, 5, 0, null, null));
    assertEquals("null", null, divide(10, 5, null, null, null));
    assertEquals("null", null, divide(10, 3, 2, RoundingMode.DOWN, null));
    assertEquals("null", (Object) 3.33f, divide(10, 3, 2, RoundingMode.DOWN, float.class));
    try {
        for (Class<?> type : PRIMITIVES) {
            Object expected = ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class)
                    .invoke(null, "0");
            assertEquals("fallback: " + type.getSimpleName(), expected,
                    divide(null, null, null, null, (Class<? extends Number>) type));
        }
        for (Class<?> type : NUMBERS) {
            Object expected = valueOf(3.33, (Class<? extends Number>) type);
            assertEquals("10 / 3: " + type.getSimpleName(), expected,
                    divide(10, 3, 2, RoundingMode.DOWN, (Class<? extends Number>) type));
            assertEquals("10 / 3: " + type.getSimpleName(), expected,
                    divide(10, 3, 2, RoundingMode.HALF_EVEN, (Class<? extends Number>) type));
            assertEquals("10 / 3: " + type.getSimpleName(), expected,
                    divide(10, 3, 2, RoundingMode.HALF_UP, (Class<? extends Number>) type));
            assertEquals("10 / 3: " + type.getSimpleName(), expected,
                    divide(10, 3, 2, RoundingMode.FLOOR, (Class<? extends Number>) type));
            expected = valueOf(3.34, (Class<? extends Number>) type);
            assertEquals("10 / 3: " + type.getSimpleName(), expected,
                    divide(10, 3, 2, RoundingMode.UP, (Class<? extends Number>) type));
            assertEquals("10 / 3: " + type.getSimpleName(), expected,
                    divide(10, 3, 2, RoundingMode.CEILING, (Class<? extends Number>) type));
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace()));
    }
}

From source file:org.libreplan.web.planner.order.OrderPlanningModel.java

private String getLabelTextEarnedValueType(OrderEarnedValueChartFiller earnedValueChartFiller,
        EarnedValueType type, LocalDate date) {

    BigDecimal value = earnedValueChartFiller.getIndicator(type, date);
    String units = _("h");
    if (type.equals(EarnedValueType.CPI) || type.equals(EarnedValueType.SPI)) {
        value = value.multiply(new BigDecimal(100));
        units = "%";
    }/*from   w w  w.  ja v a  2  s .c o m*/
    return value.setScale(0, RoundingMode.HALF_UP) + " " + units;
}

From source file:net.tradelib.misc.StrategyText.java

static private String formatBigDecimal(BigDecimal bd, int minPrecision, int maxPrecision) {
    bd = bd.setScale(maxPrecision, RoundingMode.HALF_UP).stripTrailingZeros();
    int scale = bd.scale() <= minPrecision ? minPrecision : bd.scale();
    return String.format("%,." + Integer.toString(scale) + "f", bd);
}

From source file:net.tradelib.misc.StrategyText.java

static private String formatDouble(double dd, int minPrecision, int maxPrecision) {
    BigDecimal bd = new BigDecimal(dd, MathContext.DECIMAL128);
    bd = bd.setScale(maxPrecision, RoundingMode.HALF_UP);
    return formatBigDecimal(bd);
}