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:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("16");
    BigDecimal bg2 = new BigDecimal("3");

    // divide bg1 with bg2 rounding up
    BigDecimal bg3 = bg1.divide(bg2, RoundingMode.UP);

    System.out.println(bg3);//from   w  ww .  j  a va2s . com
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123456");
    BigDecimal bg2 = new BigDecimal("3");

    MathContext mc = new MathContext(2);

    BigDecimal bg3 = bg1.divide(bg2, mc);

    System.out.println(bg3);/*from   www.  j  ava 2s  .c o  m*/
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("16");
    BigDecimal bg2 = new BigDecimal("3");

    // divide bg1 with bg2
    // 0 specifies BigDecimal.ROUND_UP
    BigDecimal bg3 = bg1.divide(bg2, BigDecimal.ROUND_UP);

    System.out.println(bg3);/*ww w .j  ava  2 s. c  om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    // Create via a string
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");

    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);

    bd1 = bd1.add(bd2);//from  w  w w.ja  v  a2 s.  co m
    bd1 = bd1.multiply(bd2);
    bd1 = bd1.subtract(bd2);
    bd1 = bd1.divide(bd2, BigDecimal.ROUND_UP);
    bd1 = bd1.negate();
}

From source file:Main.java

public static void divideBigDecimalsInMap(Map<String, Object> baseMap, BigDecimal divisor) {
    if (baseMap == null || divisor == null || divisor.doubleValue() == 0.0)
        return;/*from  w w  w  . j  a  va 2  s. c o  m*/
    for (Map.Entry<String, Object> entry : baseMap.entrySet()) {
        if (!(entry.getValue() instanceof BigDecimal))
            continue;
        BigDecimal baseVal = (BigDecimal) entry.getValue();
        entry.setValue(baseVal.divide(divisor, BigDecimal.ROUND_HALF_UP));
    }
}

From source file:Main.java

private static BigDecimal findRatio(int width, int height) {
    BigDecimal bdWidth = BigDecimal.valueOf(width);
    BigDecimal bdHeight = BigDecimal.valueOf(height);
    return bdWidth.divide(bdHeight, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(1000L));
}

From source file:resources.common.MathUtilities.java

/**
 * Converts seconds to a whole hour.//from ww  w . j a  v  a 2 s  .c o  m
 * @author Waverunner
 * @param seconds
 * @return hour(s)
 */
public static int secondsToWholeHours(int seconds) {
    BigDecimal dec = new BigDecimal(seconds);

    return dec.divide(new BigDecimal(3600), BigDecimal.ROUND_FLOOR).intValue();
}

From source file:com.qcadoo.model.internal.units.UnitConversionImpl.java

public static UnitConversion build(final Entity unitConversionItem, final MathContext mathContext) {
    final String unitFrom = unitConversionItem.getStringField(UnitConversionItemFields.UNIT_FROM);
    final String unitTo = unitConversionItem.getStringField(UnitConversionItemFields.UNIT_TO);
    final BigDecimal quantityFrom = unitConversionItem.getDecimalField(UnitConversionItemFields.QUANTITY_FROM);
    final BigDecimal quantityTo = unitConversionItem.getDecimalField(UnitConversionItemFields.QUANTITY_TO);
    final BigDecimal ratio = quantityTo.divide(quantityFrom, mathContext);

    return new UnitConversionImpl(unitFrom, unitTo, ratio, mathContext);
}

From source file:org.apache.fineract.portfolio.servicecharge.util.ServiceChargeOperationUtils.java

public static BigDecimal divideNonZeroValues(BigDecimal operand, BigDecimal divisor) {
    if (operand == null) {
        return BigDecimal.ONE;
    }/*from  www  . ja v a2s  . c  om*/
    if (divisor != null && !divisor.equals(BigDecimal.ZERO)) {
        operand = operand.divide(divisor, RoundingMode.HALF_UP);
    }
    return operand;
}

From source file:org.apache.fineract.portfolio.servicecharge.util.ServiceChargeOperationUtils.java

public static BigDecimal divideAndMultiplyNonZeroValues(BigDecimal operand, BigDecimal divisor,
        BigDecimal multiplicand) {
    if (operand == null) {
        return BigDecimal.ONE;
    }/*from   w ww. ja  v a 2 s  . c  o m*/
    if (divisor != null && !divisor.equals(BigDecimal.ZERO)) {
        operand = operand.divide(divisor, RoundingMode.HALF_UP);
    }
    if (multiplicand != null) {
        operand = operand.multiply(multiplicand);
    }
    return operand;
}