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: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   www .  j  a v  a  2  s.  com
    if (divisor != null && !divisor.equals(BigDecimal.ZERO)) {
        operand = operand.divide(divisor, RoundingMode.HALF_UP);
    }
    if (multiplicand != null) {
        operand = operand.multiply(multiplicand);
    }
    return operand;
}

From source file:Main.java

/**
 * Compute e^x to a given scale by the Taylor series.
 * @param x the value of x/*from  w w  w  .  ja va 2  s.  c  om*/
 * @param scale the desired scale of the result
 * @return the result value
 */
private static BigDecimal expTaylor(BigDecimal x, int scale) {
    BigDecimal factorial = BigDecimal.valueOf(1);
    BigDecimal xPower = x;
    BigDecimal sumPrev;

    // 1 + x
    BigDecimal sum = x.add(BigDecimal.valueOf(1));

    // Loop until the sums converge
    // (two successive sums are equal after rounding).
    int i = 2;
    do {
        // x^i
        xPower = xPower.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);

        // i!
        factorial = factorial.multiply(BigDecimal.valueOf(i));

        // x^i/i!
        BigDecimal term = xPower.divide(factorial, scale, BigDecimal.ROUND_HALF_EVEN);

        // sum = sum + x^i/i!
        sumPrev = sum;
        sum = sum.add(term);

        ++i;
        Thread.yield();
    } while (sum.compareTo(sumPrev) != 0);

    return sum;
}

From source file:com.skubit.iab.activities.TransactionDetailsActivity.java

private static final String formatCurrencyAmount(BigDecimal balanceNumber) {
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault());
    numberFormat.setMaximumFractionDigits(8);
    numberFormat.setMinimumFractionDigits(4);

    if (balanceNumber.compareTo(BigDecimal.ZERO) == -1) {
        balanceNumber = balanceNumber.multiply(new BigDecimal(-1));
    }// ww  w. j  a  v  a  2 s . c  o m

    return numberFormat.format(balanceNumber);
}

From source file:com.spaceprogram.simplejpa.util.AmazonSimpleDBUtil.java

public static BigDecimal decodeRealNumberRange(String value, int maxDigitsRight, BigDecimal offsetValue) {
    BigDecimal offsetNumber = new BigDecimal(value);
    BigDecimal shiftMultiplier = new BigDecimal(Math.pow(10, maxDigitsRight));
    BigDecimal tempVal0 = offsetValue.multiply(shiftMultiplier);
    //        System.out.println("tempVal0=" + tempVal0);
    BigDecimal tempVal = (offsetNumber.subtract(tempVal0));
    //        System.out.println("tempVal=" + tempVal);
    return (tempVal.divide(shiftMultiplier));
}

From source file:com.willetinc.hadoop.mapreduce.dynamodb.BinarySplitter.java

/**
 * Return the string encoded in a BigDecimal. Repeatedly multiply the input
 * value by 16; the integer portion after such a multiplication represents a
 * single character in base 16. Convert that back into a char and create a
 * string out of these until we have no data left.
 * /*from w  w w .  ja va2 s  . c o  m*/
 * @throws IOException
 */
static byte[] bigDecimalToByteArray(BigDecimal bd, int maxBytes) {
    BigDecimal cur = bd.stripTrailingZeros();
    ByteArrayOutputStream sb = new ByteArrayOutputStream();

    try {
        byte[] curCodePoint = new byte[1];
        for (int numConverted = 0; numConverted < maxBytes; numConverted++) {
            cur = cur.multiply(ONE_PLACE);
            curCodePoint[0] = cur.byteValue();
            if (0x0 == curCodePoint[0]) {
                break;
            }

            cur = cur.subtract(new BigDecimal(new BigInteger(curCodePoint)));
            sb.write(curCodePoint);
        }
    } catch (IOException e) {
        LOG.error("Error writing byte array", e);
    }

    return sb.toByteArray();
}

From source file:com.cmsz.cloudplatform.utils.StringUtils.java

public static String convertBytes(String bytes) {
    if (StringUtil.isNullString(bytes)) {
        return null;
    }//from ww  w  . j av  a2s .  c  o m

    // NumberFormat format = new DecimalFormat("##0.00");
    BigDecimal _1024 = new BigDecimal(1024);
    BigDecimal value = new BigDecimal(bytes);

    if (value.compareTo(_1024) == -1) {
        return value.divide(new BigDecimal(1), 2, RoundingMode.HALF_UP) + " KB";
    } else if (value.compareTo(_1024.multiply(_1024)) == -1) {
        return value.divide(_1024, 2, RoundingMode.HALF_UP) + " MB";
    } else if (value.compareTo(_1024.multiply(_1024).multiply(_1024)) == -1) {
        return value.divide(_1024).divide(_1024, 2, RoundingMode.HALF_UP) + " GB";
    } else {
        return value.divide(_1024).divide(_1024).divide(_1024, 2, RoundingMode.HALF_UP) + " TB";
    }
}

From source file:org.libreplan.business.workingday.EffortDuration.java

public static EffortDuration fromHoursAsBigDecimal(BigDecimal hours) {
    BigDecimal secondsPerHour = new BigDecimal(3600);

    return elapsing(hours.multiply(secondsPerHour).intValue(), Granularity.SECONDS);
}

From source file:com.qtplaf.library.util.NumberUtils.java

/**
 * Returns a list of increases to apply.
 * //from w  ww . j a  v a 2 s .  c om
 * @param integerDigits The number of integer digits.
 * @param decimalDigits The numbeer of decimal digits.
 * @param multipliers The list of multipliers.
 * @return The list of increases.
 */
public static List<BigDecimal> getIncreases(int integerDigits, int decimalDigits, int... multipliers) {
    List<BigDecimal> increaments = new ArrayList<>();
    int upperScale = decimalDigits;
    int lowerScale = (integerDigits - 1) * (-1);
    for (int scale = upperScale; scale >= lowerScale; scale--) {
        for (int multiplier : multipliers) {
            BigDecimal value = NumberUtils.getBigDecimal(Math.pow(10, -scale), scale);
            BigDecimal multiplicand = new BigDecimal(multiplier).setScale(0, BigDecimal.ROUND_HALF_UP);
            increaments.add(value.multiply(multiplicand));
        }
    }
    return increaments;
}

From source file:to.sparks.mtgox.example.TradingBot.java

private static boolean isDiffTooLarge(BigDecimal actualPrice, BigDecimal optimumPrice) {
    BigDecimal diff;//  ww w  .  ja va2s  .c  o m
    if (actualPrice.compareTo(optimumPrice) < 0) {
        diff = optimumPrice.subtract(actualPrice);
    } else {
        diff = actualPrice.subtract(optimumPrice);
    }
    return diff.compareTo(optimumPrice.multiply(percentAllowedPriceDeviation)) > 0;
}

From source file:org.cirdles.ambapo.LatLongToUTM.java

/**
 * The meridian radius is based on the lines that run north-south on a map
 * UTM easting coordinates are referenced to the center line of the zone 
 * known as the central meridian//from w  ww  . j av a 2 s .co m
 * The central meridian is assigned an 
 * easting value of 500,000 meters East.
 * @param meridianRadius
 * @param etaEast
 * @param longitude
 * @param centralMeridian
 * @return BigDecimal easting
 * 
 */
private static BigDecimal calcEasting(BigDecimal meridianRadius, BigDecimal etaEast, BigDecimal longitude,
        BigDecimal centralMeridian) {

    BigDecimal easting = (SCALE_FACTOR.multiply(meridianRadius)).multiply(etaEast);
    BigDecimal eastOfCM = BigDecimal.ONE;

    if (longitude.compareTo(centralMeridian) < 0)
        eastOfCM = eastOfCM.multiply(new BigDecimal(-1));

    easting = FALSE_EASTING.add(eastOfCM.multiply(easting));

    return easting;
}