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, int scale, RoundingMode roundingMode) 

Source Link

Document

Returns a BigDecimal whose value is (this / divisor) , and whose scale is as specified.

Usage

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

public static String convertBytes(String bytes) {
    if (StringUtil.isNullString(bytes)) {
        return null;
    }/* ww w .j ava  2s  .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:biz.c24.io.spring.integration.samples.fpml.FpmlGenerator.java

private static TradeConfirmed randomizeFpML(TradeConfirmed tradeConfirmed) throws CloneNotSupportedException {
    ISO8601DateTime creationDate = new ISO8601DateTime(new Date());
    ISO8601Date tradeDate = new ISO8601Date(new GregorianCalendar(2011, Calendar.JULY, 5).getTime());
    ISO8601Date expiryDate = new ISO8601Date(new GregorianCalendar(2012, Calendar.JANUARY, 5).getTime());
    ISO8601Date settlementDate = new ISO8601Date(new GregorianCalendar(2011, Calendar.JULY, 7).getTime());
    ISO8601Date valueDate = new ISO8601Date(new GregorianCalendar(2012, Calendar.JANUARY, 9).getTime());

    Random random = new Random();

    BigDecimal putAmount = new BigDecimal((1 + random.nextInt(999)) * 100000.);
    BigDecimal callAmount = new BigDecimal((1 + random.nextInt(999)) * 100000.);
    BigDecimal fxRate = callAmount.divide(putAmount, 8, RoundingMode.HALF_EVEN).setScale(5,
            BigDecimal.ROUND_HALF_UP);
    BigDecimal premiumValue = new BigDecimal(0.001).setScale(5, BigDecimal.ROUND_HALF_UP);
    BigDecimal premiumAmount = callAmount.multiply(premiumValue).setScale(2, BigDecimal.ROUND_HALF_UP);

    String sendTo = "SendTo" + random.nextInt(999999);
    String sentBy = "SentBy" + random.nextInt(999999);
    String msgId = "Msg" + random.nextInt(999999);
    String conversationId = "Conv" + random.nextInt(999999);
    String tradeId1 = "ID1:" + random.nextInt(999999);
    String tradeId2 = "ID2:" + random.nextInt(999999);

    // Set the header
    NotificationMessageHeader header = tradeConfirmed.getHeader();
    header.getMessageHeadermodel().setCreationTimestamp(creationDate);
    header.getConversationId().setValue(conversationId);
    header.getMessageId().setValue(msgId);
    header.getMessageHeadermodel().getSentBy().setValue(sentBy);
    header.getMessageHeadermodel().getSendTo()[0].setValue(sendTo);

    // Set the TradeHeader
    TradeHeader tradeHeader = tradeConfirmed.getTrade().getTradeHeader();
    tradeHeader.getPartyTradeIdentifier()[0].getTradeIdentifierSG1()[0].getTradeId().setValue(tradeId1);
    tradeHeader.getPartyTradeIdentifier()[1].getTradeIdentifierSG1()[0].getTradeId().setValue(tradeId2);
    tradeHeader.getTradeDate().setValue(tradeDate);

    FxOptionLeg fxOptionLeg = (FxOptionLeg) tradeConfirmed.getTrade().getProduct();
    fxOptionLeg.getExpiryDateTime().setExpiryDate(expiryDate);

    FxOptionPremium optionPremium = fxOptionLeg.getFxOptionPremium()[0];
    optionPremium.getPremiumAmount().setAmount(premiumAmount);
    optionPremium.setPremiumSettlementDate(settlementDate);
    SettlementInstruction settlementInstruction = optionPremium.getSettlementInformation()
            .getSettlementInstruction();
    settlementInstruction.getCorrespondentInformation().getRoutingIdentificationmodel().getRoutingIds()
            .getRoutingId()[0].setValue(sendTo);
    settlementInstruction.getBeneficiary().getRoutingIdentificationmodel().getRoutingIds().getRoutingId()[0]
            .setValue(sentBy);/*ww w .  j ava  2 s  . c o  m*/
    optionPremium.getPremiumQuote().setPremiumValue(premiumValue);
    fxOptionLeg.setValueDate(valueDate);

    fxOptionLeg.getPutCurrencyAmount().getCurrency().setValue("AUD");
    fxOptionLeg.getPutCurrencyAmount().setAmount(putAmount);

    fxOptionLeg.getPutCurrencyAmount().getCurrency().setValue("USD");
    fxOptionLeg.getCallCurrencyAmount().setAmount(callAmount);

    fxOptionLeg.getFxStrikePrice().setRate(fxRate);

    fxOptionLeg.getQuotedAs().getOptionOnCurrency().setValue("AUD");
    fxOptionLeg.getQuotedAs().getFaceOnCurrency().setValue("USD");
    fxOptionLeg.getQuotedAs().getQuotedTenor().setPeriod("M");
    fxOptionLeg.getQuotedAs().getQuotedTenor().setPeriodMultiplier(new BigInteger("6"));

    tradeConfirmed.getParty()[0].getPartyId()[0].setValue(sendTo);
    tradeConfirmed.getParty()[1].getPartyId()[0].setValue(sentBy);

    return (TradeConfirmed) tradeConfirmed.cloneDeep();
}

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

public static String convertHz(String hz) {
    if (StringUtil.isNullString(hz)) {
        return null;
    }/* w w w.ja  v a2  s. com*/

    // NumberFormat format = new DecimalFormat("##0.00");
    BigDecimal _1000 = new BigDecimal(1000);
    BigDecimal value = new BigDecimal(hz);

    if (value.compareTo(_1000) == -1) {
        return value + " MHz";
    } else {
        return value.divide(_1000, 2, RoundingMode.HALF_UP) + " GHz";
    }
}

From source file:Main.java

/**
 * Compute e^x to a given scale. Break x into its whole and fraction parts
 * and compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * //from   w w w  .ja  v a 2s. c o  m
 * @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: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;/*  w w  w. j  av a 2 s.c om*/

    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;// w  w  w .j a 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:Main.java

public static BigDecimal sine(BigDecimal x) {
    BigDecimal lastVal = x.add(BigDecimal.ONE);
    BigDecimal currentValue = x;/* w  ww  . j av a  2s . com*/
    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;/*from ww  w. j  a v a  2 s .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.add(term);
        } else {
            currentValue = currentValue.subtract(term);
        }

        i++;
    }
    return currentValue;
}

From source file:com.whatlookingfor.common.utils.StringUtils.java

/**
 * ??????//from ww  w  . j av  a 2 s .  co  m
 *
 * @param v     ??
 * @param scale ????
 * @return ??
 */
public static double round(double v, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }
    BigDecimal b = new BigDecimal(Double.toString(v));
    BigDecimal one = new BigDecimal("1");
    return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:com.whatlookingfor.common.utils.StringUtils.java

/**
 * ?????scale?// ww  w  .  jav  a  2  s.c  o  m
 * ??
 *
 * @param v1    
 * @param v2    
 * @param scale ????
 * @return ?
 */
public static double div(double v1, double v2, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}