Example usage for java.math BigDecimal multiply

List of usage examples for java.math BigDecimal multiply

Introduction

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

Prototype

public BigDecimal multiply(BigDecimal multiplicand) 

Source Link

Document

Returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()) .

Usage

From source file:Main.java

public static BigDecimal subtractVatAmount(BigDecimal percentage, BigDecimal amount) {
    BigDecimal f = BigDecimal.ONE
            .subtract(BigDecimal.ONE.divide(percentage.add(HUNDRED).divide(HUNDRED), 14, RoundingMode.HALF_UP));
    return amount.multiply(f).setScale(2, RoundingMode.HALF_UP);
}

From source file:Main.java

public static double mulddd(double value1, double value2, double value3) {
    BigDecimal b1 = new BigDecimal(Double.toString(value1));
    BigDecimal b2 = new BigDecimal(Double.toString(value2));
    BigDecimal b3 = new BigDecimal(Double.toString(value3));
    return b1.multiply(b2).multiply(b3).doubleValue();
}

From source file:com.surfs.storage.web.utils.Stringutils.java

public static String convertToKB(String total) {
    if (StringUtils.isBlank(total)) {
        return total;
    }//ww  w.  ja v  a  2 s.  com

    String space = total.substring(0, total.length() - 1);
    String unit = total.substring(total.length() - 1, total.length());

    BigDecimal t = new BigDecimal(space);
    BigDecimal multiply = null;

    if (unit.equalsIgnoreCase("M")) {
        multiply = t.multiply(new BigDecimal("1048576"));
    } else if (unit.equalsIgnoreCase("T")) {
        multiply = t.multiply(new BigDecimal("1099511627776"));
    } else if (unit.equalsIgnoreCase("G")) {
        multiply = t.multiply(new BigDecimal("1073741824"));
    }

    return String.valueOf(multiply.doubleValue());
}

From source file:Main.java

/**
 * Multiplies two decimals and applies the given scale and a ROUND_HALF_UP. This method should be used only for the final result
 * calculation.For example if we have something like this: (axb)/c the rules should be applied to the result of the division
 * only and not all the computations that give us the final result.
 * /*from w  w w  . jav a 2 s.co m*/
 * @param multiplier1 first multiplier
 * @param multiplier2 second multiplier
 * @param scale the scale fo the result
 * @return the result of the multiplication after scale and rounding are applied
 */
public static BigDecimal multiply(BigDecimal multiplier1, BigDecimal multiplier2, int scale) {

    BigDecimal result = BigDecimal.ZERO;

    if (multiplier1 != null && multiplier2 != null) {
        result = multiplier1.multiply(multiplier2);
    }

    result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
    return result;
}

From source file:client.Pi.java

/**
 * Compute the value of pi to the specified number of 
 * digits after the decimal point.  The value is 
 * computed using Machin's formula://from www . j a v  a  2 s . com
 *
 *          pi/4 = 4*arctan(1/5) - arctan(1/239)
 *
 * and a power series expansion of arctan(x) to 
 * sufficient precision.
 */
public static BigDecimal computePi(int digits) {
    int scale = digits + 5;
    BigDecimal arctan1_5 = arctan(5, scale);
    BigDecimal arctan1_239 = arctan(239, scale);
    BigDecimal pi = arctan1_5.multiply(FOUR).subtract(arctan1_239).multiply(FOUR);
    return pi.setScale(digits, BigDecimal.ROUND_HALF_UP);
}

From source file:Main.java

public static BigDecimal cosine(BigDecimal x) {

    BigDecimal currentValue = BigDecimal.ONE;
    BigDecimal lastVal = currentValue.add(BigDecimal.ONE);
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = BigDecimal.ONE;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;/*from   ww  w.  j  a v a 2s . c  o m*/

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 2;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 == 0) {
            currentValue = currentValue.subtract(term);
        } else {
            currentValue = currentValue.add(term);
        }
        i++;
    }

    return currentValue;
}

From source file:Main.java

public static BigDecimal cosine(BigDecimal x) {

    BigDecimal currentValue = BigDecimal.ONE;
    BigDecimal lastVal = currentValue.add(BigDecimal.ONE);
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = BigDecimal.ONE;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;/*from  w  ww.ja  v  a 2 s.  co  m*/

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 2;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 != 0) {
            currentValue = currentValue.add(term);
        } else {
            currentValue = currentValue.subtract(term);
        }
        i++;
    }

    return currentValue;
}

From source file:gov.nih.nci.calims2.ui.administration.customerservice.serviceitem.ServiceItemHelper.java

/**
 * Give the sub total value from quantity and rate.
 * //from  w  ww  .j  a va  2 s.co m
 * @param row The ServiceItem type
 * @return The sub total corresponding to the given value. Returns null if the subtotal can not be calculated
 */
public static BigDecimal getsubTotalValue(ServiceItem row) {
    Quantity quantity = row.getQuantity();
    Rate rate = row.getServiceItemRate();
    if (quantity == null || StringUtils.isBlank(quantity.getValue()) || rate == null
            || rate.getQuantity() == null || StringUtils.isBlank(rate.getQuantity().getValue())) {
        return null;
    }
    BigDecimal bigDecQty = new BigDecimal(quantity.getValue());
    BigDecimal bigDecRate = new BigDecimal(rate.getQuantity().getValue());
    BigDecimal subtotal = bigDecQty.multiply(bigDecRate);
    return subtotal;
}

From source file:Main.java

public static BigDecimal sine(BigDecimal x) {
    BigDecimal lastVal = x.add(BigDecimal.ONE);
    BigDecimal currentValue = x;//w  w  w  .ja  va 2s  . c  o m
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = x;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 3;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 == 0) {
            currentValue = currentValue.subtract(term);
        } else {
            currentValue = currentValue.add(term);
        }

        i++;
    }
    return currentValue;
}

From source file:Main.java

public static BigDecimal sine(BigDecimal x) {
    BigDecimal lastVal = x.add(BigDecimal.ONE);
    BigDecimal currentValue = x;//  w ww.  j a  v  a 2  s  .co m
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = x;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 3;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 != 0) {
            currentValue = currentValue.add(term);
        } else {
            currentValue = currentValue.subtract(term);
        }

        i++;
    }
    return currentValue;
}