Example usage for java.math BigDecimal subtract

List of usage examples for java.math BigDecimal subtract

Introduction

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

Prototype

public BigDecimal subtract(BigDecimal subtrahend) 

Source Link

Document

Returns a BigDecimal whose value is (this - subtrahend) , and whose scale is max(this.scale(), subtrahend.scale()) .

Usage

From source file:com.fengduo.bee.commons.util.NumberParser.java

public static double sub(double a, double b) {
    BigDecimal b1 = new BigDecimal(Double.toString(a));
    BigDecimal b2 = new BigDecimal(Double.toString(b));
    return b1.subtract(b2).doubleValue();
}

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

public static BigDecimal decodeRealNumberRange(String value, BigDecimal offsetValue) {
    BigDecimal offsetNumber = new BigDecimal(value);
    return (offsetNumber.subtract(offsetValue));
}

From source file:org.techytax.helper.AmountHelper.java

private static void applyVat(Cost cost, VatType vatType) throws Exception {
    BigDecimal amount = cost.getAmount();
    if (amount != null) {
        BigDecimal bd = new BigDecimal(amount.doubleValue() / (1 + vatType.getValue(cost.getDate())));
        bd = round(bd);/* www  .ja va  2 s.c  om*/
        cost.setAmount(bd);
        cost.setVat(amount.subtract(bd));
    }
}

From source file:com.nearinfinity.honeycomb.hbase.bulkload.FieldParser.java

private static ByteBuffer extractDecimal(String val, int precision, int right_scale) {
    int left_scale = precision - 2;
    BigDecimal x = new BigDecimal(val);
    boolean is_negative = x.compareTo(BigDecimal.ZERO) == -1;
    x = x.abs();// w  w  w . ja v  a2  s .co  m
    BigDecimal left = x.setScale(0, RoundingMode.DOWN);
    BigDecimal right = x.subtract(left).movePointRight(right_scale);
    int right_bytes_len = bytesFromDigits(right_scale);
    int left_bytes_len = bytesFromDigits(left_scale);
    byte[] left_bytes = left.toBigInteger().toByteArray();
    byte[] right_bytes = right.toBigInteger().toByteArray();
    // Bit twiddling is fun
    byte[] buff = new byte[left_bytes_len + right_bytes_len];

    System.arraycopy(left_bytes, 0, buff, left_bytes_len - left_bytes.length, left_bytes.length);
    System.arraycopy(right_bytes, 0, buff, right_bytes_len - right_bytes.length + left_bytes_len,
            right_bytes.length);

    buff[0] ^= -128; // Flip first bit, 0x80
    if (is_negative) { // Flip all bits
        for (int i = 0; i < buff.length; i++) {
            buff[i] ^= -1; // 0xff
        }
    }
    return ByteBuffer.wrap(buff);
}

From source file:Util.java

public static BigDecimal[] quadratic(BigDecimal a, BigDecimal b, BigDecimal c, RoundingMode rounding) {
    // ax^2 + bx + c = 0
    BigDecimal part1 = b.negate();
    BigDecimal part2 = sqrt(b.multiply(b).subtract(BigDecimalFOUR.multiply(a).multiply(c)), rounding);
    BigDecimal part3 = BigDecimalTWO.multiply(a);
    BigDecimal[] toRet = new BigDecimal[2];
    toRet[0] = (part1.subtract(part2)).divide(part3, RoundingMode.CEILING);
    toRet[1] = (part1.add(part2)).divide(part3, RoundingMode.CEILING);
    return toRet;
}

From source file:Main.java

/**
 * Compute the natural logarithm of x to a given scale, x > 0.
 * Use Newton's algorithm./*from ww w  .  j  a v  a2 s . co m*/
 */
private static BigDecimal lnNewton(BigDecimal x, int scale) {
    int sp1 = scale + 1;
    BigDecimal n = x;
    BigDecimal term;

    // Convergence tolerance = 5*(10^-(scale+1))
    BigDecimal tolerance = BigDecimal.valueOf(5).movePointLeft(sp1);

    // Loop until the approximations converge
    // (two successive approximations are within the tolerance).
    do {

        // e^x
        BigDecimal eToX = exp(x, sp1);

        // (e^x - n)/e^x
        term = eToX.subtract(n).divide(eToX, sp1, BigDecimal.ROUND_DOWN);

        // x - (e^x - n)/e^x
        x = x.subtract(term);

        Thread.yield();
    } while (term.compareTo(tolerance) > 0);

    return x.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}

From source file:org.egov.infra.gis.service.GeoLocationService.java

private static BigDecimal getRangeSize(BigDecimal wardDataMinAmount, BigDecimal wardDataMaxAmount,
        int totalNoOfColors) {

    BigDecimal rangeSize = (wardDataMaxAmount.subtract(wardDataMinAmount))
            .divide(BigDecimal.valueOf(totalNoOfColors), BigDecimal.ROUND_HALF_UP);
    return rangeSize;
}

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

private static boolean isDiffTooLarge(BigDecimal actualPrice, BigDecimal optimumPrice) {
    BigDecimal diff;//from w ww .  j  a  v  a2s.  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:Main.java

/**
 * Return just the fractional part of the seconds component of a duration object,
 * i.e. 1:2:34.45 => 0.45//from   w w  w .  j a  v  a  2  s .  c  o  m
 * @param duration The duration object to examine.
 * @return The fractional part of the seconds field.
 */
private static BigDecimal fractionalSeconds(Duration duration) {
    BigDecimal seconds = (BigDecimal) duration.getField(DatatypeConstants.SECONDS);

    return seconds.subtract(new BigDecimal(seconds.toBigInteger()));
}

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

/**
 * Eta-east refers to the east west direction of the UTM.
 * @param easting/*from  www . j  av  a  2s.c  om*/
 * @param meridianRadius
 * @return eta east
 */
private static BigDecimal calcEtaEast(BigDecimal easting, BigDecimal meridianRadius) {

    BigDecimal etaEast = (easting.subtract(FALSE_EASTING)).divide(SCALE_FACTOR.multiply(meridianRadius),
            PRECISION, RoundingMode.HALF_UP);

    return etaEast;
}