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 double kgToLB_ForFatScale2(double paramDouble) {
    BigDecimal localBigDecimal1 = new BigDecimal(String.valueOf(paramDouble));
    BigDecimal localBigDecimal2 = new BigDecimal("1155845");
    BigDecimal localBigDecimal3 = new BigDecimal("16");
    BigDecimal localBigDecimal4 = new BigDecimal("65536");
    BigDecimal localBigDecimal5 = new BigDecimal("2");
    return new BigDecimal(String.valueOf(localBigDecimal1.multiply(localBigDecimal2).doubleValue()))
            .divide(localBigDecimal3, 5, 6).divide(localBigDecimal4, 1, 4).multiply(localBigDecimal5)
            .doubleValue();/*from w  ww  .  j  a va  2  s . c  o m*/
}

From source file:engine.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  w  w  w .j av a 2  s.co  m*/
     *
     *          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:com.konakart.util.TaxUtils.java

/**
 * Calculates the tax for one or more items
 * /*from  ww w  . ja  v  a 2s. com*/
 * @param taxRate
 *            tax rate as a percentage
 * @param cost
 *            cost of a single item
 * @param quantity
 *            Number of items
 * @param scale
 *            This is the scale used for the precision of the calculations. It is contained in
 *            the ADMIN_CURRENCY_DECIMAL_PLACES configuration variable.
 * @param rule
 *            The rule to be used which should be either TAX_PER_ITEM or TAX_ON_TOTAL.
 *            <ul>
 *            <li>
 *            TaxUtils.TAX_PER_ITEM : The tax is calculated for a single item, to the number of
 *            decimal places defined by scale. Then this value is multiplied by the quantity.
 *            <li>
 *            TaxUtils.TAX_ON_TOTAL : The tax is calculated for the total amount (single item
 *            cost x quantity).
 *            </ul>
 * @return Returns the tax amount
 * 
 */
public static BigDecimal getTaxAmount(BigDecimal taxRate, BigDecimal cost, int quantity, int scale, int rule) {
    if (taxRate == null || cost == null || quantity == 0) {
        return new BigDecimal(0);
    }

    BigDecimal lTaxRate = taxRate.divide(new BigDecimal(100));

    if (rule == TAX_PER_ITEM) {
        BigDecimal taxPerItem = cost.multiply(lTaxRate);
        taxPerItem = taxPerItem.setScale(scale, BigDecimal.ROUND_HALF_UP);
        BigDecimal totalTax = taxPerItem.multiply(new BigDecimal(quantity));
        totalTax = totalTax.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return totalTax;
    } else if (rule == TAX_ON_TOTAL) {
        BigDecimal totalPrice = cost.multiply(new BigDecimal(quantity));
        BigDecimal totalTax = totalPrice.multiply(lTaxRate);
        totalTax = totalTax.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return totalTax;
    }

    // Should never get this far
    return new BigDecimal(0);
}

From source file:com.omricat.yacc.data.model.Currency.java

/**
 * Converts a conversionRate in the source currency to a conversionRate in
 * the target currency/*from  w w w.j a  v  a  2s  . c  o m*/
 *
 * @param source the source currency. Must be non-null.
 * @param target the target currency. Must be non-null.
 * @param value  the conversionRate to be converted. This is a
 *               conversionRate in the source currency. It should be
 *               non-negative.
 * @return the conversionRate converted into the target currency
 */
@NotNull
public static BigDecimal convert(@NotNull final Currency source, @NotNull final Currency target,
        @NotNull final BigDecimal value) {
    checkArgument(checkNotNull(value).compareTo(BigDecimal.ZERO) >= 0, "Value parameter must be non-negative");

    return value.multiply(conversionRatio(checkNotNull(source), checkNotNull(target)));
}

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

/**
 * //from  ww  w.ja v a 2s.co m
 * @param currentTau
 * @param currentSigma
 * @param originalTau
 * @return function of tau
 */
private static BigDecimal functionOfTau(BigDecimal currentTau, BigDecimal currentSigma,
        BigDecimal originalTau) {

    BigDecimal funcOfTau = originalTau
            .multiply(new BigDecimal(Math.sqrt(1 + currentSigma.pow(2).doubleValue())))
            .subtract(currentSigma.multiply(new BigDecimal(Math.sqrt(1 + currentTau.pow(2).doubleValue()))))
            .subtract(originalTau);

    return funcOfTau;

}

From source file:Main.java

public static String kgToLB_ForFatScale(float paramFloat) {
    BigDecimal localBigDecimal1 = new BigDecimal(String.valueOf(paramFloat));
    BigDecimal localBigDecimal2 = new BigDecimal("1155845");
    BigDecimal localBigDecimal3 = new BigDecimal("16");
    BigDecimal localBigDecimal4 = new BigDecimal("65536");
    BigDecimal localBigDecimal5 = new BigDecimal("2");
    return String
            .valueOf(new BigDecimal(String.valueOf(localBigDecimal1.multiply(localBigDecimal2).doubleValue()))
                    .divide(localBigDecimal3, 5, 6).divide(localBigDecimal4, 1, 4).multiply(localBigDecimal5)
                    .floatValue());/*  w  w w. j a v a 2 s  .  c  o  m*/
}

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

private static BigDecimal calcEasting(BigDecimal meridianRadius, BigDecimal etaEast, BigDecimal longitude,
        BigDecimal centralMeridian) {

    BigDecimal easting = (scaleFactor.multiply(meridianRadius)).multiply(etaEast);
    BigDecimal eastOfCM = one;

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

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

    return easting;
}

From source file:helpers.Methods.java

/**
 * This method converts human readable file size to bytes (1024).
 *
 * @param humanReadableFileSize The human readable file size (Ex. 500MB
 * <b>without space between size and unit</b>)
 * @return the bytes of human readable size in long format.
 *///from w  ww  .j a  v a  2s .co  m
public static long filesizeToBytes(String humanReadableFileSize) {
    long returnValue = -1;
    java.util.regex.Pattern patt = java.util.regex.Pattern.compile("([\\d.]+)([GMK]B)",
            java.util.regex.Pattern.CASE_INSENSITIVE);
    Matcher matcher = patt.matcher(humanReadableFileSize);
    Map<String, Integer> powerMap = new HashMap<>();
    powerMap.put("TB", 4);
    powerMap.put("GB", 3);
    powerMap.put("MB", 2);
    powerMap.put("KB", 1);
    powerMap.put("B", 0);
    if (matcher.find()) {
        String number = matcher.group(1);
        int pow = powerMap.get(matcher.group(2).toUpperCase());
        BigDecimal bytes = new BigDecimal(number);
        bytes = bytes.multiply(BigDecimal.valueOf(1024).pow(pow));
        returnValue = bytes.longValue();
    }
    return returnValue;
}

From source file:Main.java

/**
 * Compute e^x to a given scale./*from w  ww .  ja  v  a  2  s .com*/
 * Break x into its whole and fraction parts and
 * compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * @param x the value of x
 * @param scale the desired scale of the result
 * @return the result value
 */
public static BigDecimal exp(BigDecimal x, int scale) {
    // e^0 = 1
    if (x.signum() == 0) {
        return BigDecimal.valueOf(1);
    }

    // If x is negative, return 1/(e^-x).
    else if (x.signum() == -1) {
        return BigDecimal.valueOf(1).divide(exp(x.negate(), scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    // Compute the whole part of x.
    BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN);

    // If there isn't a whole part, compute and return e^x.
    if (xWhole.signum() == 0)
        return expTaylor(x, scale);

    // Compute the fraction part of x.
    BigDecimal xFraction = x.subtract(xWhole);

    // z = 1 + fraction/whole
    BigDecimal z = BigDecimal.valueOf(1).add(xFraction.divide(xWhole, scale, BigDecimal.ROUND_HALF_EVEN));

    // t = e^z
    BigDecimal t = expTaylor(z, scale);

    BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE);
    BigDecimal result = BigDecimal.valueOf(1);

    // Compute and return t^whole using intPower().
    // If whole > Long.MAX_VALUE, then first compute products
    // of e^Long.MAX_VALUE.
    while (xWhole.compareTo(maxLong) >= 0) {
        result = result.multiply(intPower(t, Long.MAX_VALUE, scale)).setScale(scale,
                BigDecimal.ROUND_HALF_EVEN);
        xWhole = xWhole.subtract(maxLong);

        Thread.yield();
    }
    return result.multiply(intPower(t, xWhole.longValue(), scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}

From source file:org.bankinterface.util.Utils.java

/**
 * ??,??,,??./*from   w  w  w  .  j ava  2s . co  m*/
 * 
 * @param amount
 * @return
 */
public static String amountToFen(BigDecimal amount) {
    if (amount == null) {
        throw new IllegalArgumentException();
    }
    return amount.multiply(ONE_HUNDRED).setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString();
}