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:org.fede.calculator.service.InvestmentServiceImpl.java

private static BigDecimal pctChange(BigDecimal now, BigDecimal then) {
    if (then.signum() == 0) {
        return ONE;
    }//  w  w  w  . j a  va  2s .c om
    return now.subtract(then).divide(then, CONTEXT);
}

From source file:Main.java

public static Duration subtract(XMLGregorianCalendar x1, XMLGregorianCalendar x2) {
    boolean positive = x1.compare(x2) >= 0;

    if (!positive) {
        XMLGregorianCalendar temp = x1;
        x1 = x2;/*ww w.j a v  a  2 s . c  om*/
        x2 = temp;
    }

    BigDecimal s1 = getSeconds(x1);
    BigDecimal s2 = getSeconds(x2);
    BigDecimal seconds = s1.subtract(s2);
    if (seconds.compareTo(BigDecimal.ZERO) < 0)
        seconds = seconds.add(BigDecimal.valueOf(60));

    GregorianCalendar g1 = x1.toGregorianCalendar();
    GregorianCalendar g2 = x2.toGregorianCalendar();

    int year = 0;
    for (int f : reverseFields) {
        if (f == Calendar.YEAR) {
            int year1 = g1.get(f);
            int year2 = g2.get(f);
            year = year1 - year2;
        } else {
            subtractField(g1, g2, f);
        }
    }

    return FACTORY.newDuration(positive, BigInteger.valueOf(year), BigInteger.valueOf(g1.get(Calendar.MONTH)),
            BigInteger.valueOf(g1.get(Calendar.DAY_OF_MONTH) - 1),
            BigInteger.valueOf(g1.get(Calendar.HOUR_OF_DAY)), BigInteger.valueOf(g1.get(Calendar.MINUTE)),
            seconds);
}

From source file:Main.java

/**
 * Subtract one positive Duration from another, larger positive Duration.
 * @param d1 The larger positive duration
 * @param d2 The smaller positive duration
 * @return The difference/*  w  w w. j  a v a2  s  .  c o m*/
 */
private static Duration subtractSmallerPositiveDurationFromLargerPositiveDuration(Duration d1, Duration d2) {
    BigDecimal s1 = fractionalSeconds(d1);
    BigDecimal s2 = fractionalSeconds(d2);
    BigDecimal extraSeconds = s1.subtract(s2);

    Duration strip1 = stripFractionalSeconds(d1);
    Duration strip2 = stripFractionalSeconds(d2);

    Duration stripResult = strip1.subtract(strip2);

    if (extraSeconds.compareTo(BigDecimal.ZERO) < 0) {
        stripResult = stripResult.subtract(DURATION_1_SECOND);
        extraSeconds = extraSeconds.add(BigDecimal.ONE);
    }

    BigDecimal properSeconds = BigDecimal.valueOf(stripResult.getSeconds()).add(extraSeconds);

    return FACTORY.newDuration(true, BigInteger.valueOf(stripResult.getYears()),
            BigInteger.valueOf(stripResult.getMonths()), BigInteger.valueOf(stripResult.getDays()),
            BigInteger.valueOf(stripResult.getHours()), BigInteger.valueOf(stripResult.getMinutes()),
            properSeconds);
}

From source file:org.springframework.data.simpledb.attributeutil.AmazonSimpleDBUtil.java

public static BigDecimal decodeRealNumberRange(String value, int maxDigitsRight, BigDecimal offsetValue) {
    BigDecimal offsetNumber = new BigDecimal(value);
    BigDecimal shiftMultiplier = new BigDecimal(Math.pow(BASE, maxDigitsRight));
    BigDecimal tempVal0 = offsetValue.multiply(shiftMultiplier);
    BigDecimal tempVal = (offsetNumber.subtract(tempVal0));
    return (tempVal.divide(shiftMultiplier));
}

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

/**
 * //  ww  w . j a  v a 2s. c  o  m
 * @param wardWiseData Map<String, Integer>- Map having the key as the ward number and value as the no of complaints/properties/assets
 *    in that ward. e.g [<Ward Number>,<no Of Complaints/assets/properties> ]
 * 
 * @param colorCodes Map<Integer,String> - Map having colour codes , the key as the the colour priority and value as the colour 
 * code in RGB format. e.g - key = 1 , means the associated colour to represent the ward having  no of complaints/assets/properties
 * that falls in highest range , key = 2 means associated colour to represent ward having  no of complaints/assets/properties
 * that falls in the 2nd highest range and so on.
 * example :  (1, "FF0000");
 *           (2, "8968CD");
 *            (3, "FFA500");
 *            (4, "4169E1");
 *            (5, "008B00");
 */

public static void setKmlDataToCacheAndRequest(Map<String, BigDecimal> wardWiseData,
        Map<Integer, String> colorCodes, String kmlPath, HttpServletRequest request) {

    LOGGER.debug("GeoLocationService | setKmlDataToCacheAndRequest | Start");

    int totalNoOfColors = colorCodes.size();
    BigDecimal wardDataMinAmount = Collections.min(wardWiseData.values()).setScale(0, BigDecimal.ROUND_HALF_UP); // to hold the minimum amount in all the wards.
    BigDecimal wardDataMaxAmount = Collections.max(wardWiseData.values()).setScale(0, BigDecimal.ROUND_HALF_UP); // to hold the maximum amount in all the wards.
    if ((wardDataMaxAmount.subtract(wardDataMinAmount)).compareTo(BigDecimal.valueOf(totalNoOfColors)) == -1) {
        throw new ValidationException(Arrays.asList(new ValidationError("colorrange",
                "no of colors supplied is more than the range of data " + "in the wards")));
    }

    BigDecimal rangeSize = getRangeSize(wardDataMinAmount, wardDataMaxAmount, totalNoOfColors);

    GeoKmlInfo geoKmlInfo = new GeoKmlInfo();
    Map<String, String> wardWiseKmlColorStyle = new HashMap<String, String>();

    for (Map.Entry<String, BigDecimal> entry : wardWiseData.entrySet()) {
        wardWiseKmlColorStyle.put("style" + entry.getKey(),
                getStyleColorName(entry.getValue(), wardDataMinAmount, totalNoOfColors, rangeSize));
    }

    geoKmlInfo.setWardWiseColor(wardWiseKmlColorStyle);
    geoKmlInfo.setColorCodes(convertToKmlColor(colorCodes));

    request.setAttribute(GeoLocationConstants.KML_DATA_MODEL_JBOSS_CACHE_KEY_NAME,
            putKmlDataToCache(geoKmlInfo));
    request.setAttribute(GeoLocationConstants.COLOR_CODE_AND_RANGE_MAP_NAME,
            getColorRange(wardDataMinAmount, wardDataMaxAmount, rangeSize, colorCodes));
    if (null != kmlPath && StringUtils.isNotEmpty(kmlPath)) {
        request.setAttribute(GeoLocationConstants.KML_URL_PATH_REQ_ATTR_NAME, kmlPath);
    }

    LOGGER.debug("GeoLocationService | setKmlDataToCacheAndRequest | End");

}

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

private static String getStyleColorName(BigDecimal wardWiseNos, BigDecimal wardDataMinAmount,
        Integer totalNoOfColors, BigDecimal rangeSize) {

    return "#color" + (BigDecimal.valueOf(totalNoOfColors)
            .subtract((wardWiseNos.subtract(wardDataMinAmount).subtract(BigDecimal.ONE)).divide(rangeSize, 0,
                    BigDecimal.ROUND_DOWN)));

}

From source file:org.fede.calculator.service.InvestmentServiceImpl.java

private static BigDecimal savingsPct(BigDecimal savingsNow, BigDecimal savingsThen, BigDecimal avgIncome) {
    if (savingsNow.compareTo(savingsThen) == 0) {
        return ZERO;
    }/*w w w .j a v  a2  s.c om*/
    return savingsNow.subtract(savingsThen).divide(avgIncome, CONTEXT);
}

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.whatlookingfor.common.utils.StringUtils.java

/**
 * ????//from  w  w w. ja  v a 2 s .c  o  m
 *
 * @param v1 ?
 * @param v2 ?
 * @return ?
 */

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

From source file:org.apache.hadoop.hive.serde2.io.TimestampWritable.java

public static Timestamp doubleToTimestamp(double f) {
    long seconds = (long) f;

    // We must ensure the exactness of the double's fractional portion.
    // 0.6 as the fraction part will be converted to 0.59999... and
    // significantly reduce the savings from binary serializtion
    BigDecimal bd = new BigDecimal(String.valueOf(f));
    bd = bd.subtract(new BigDecimal(seconds)).multiply(new BigDecimal(1000000000));
    int nanos = bd.intValue();

    // Convert to millis
    long millis = seconds * 1000;
    Timestamp t = new Timestamp(millis);

    // Set remaining fractional portion to nanos
    t.setNanos(nanos);/*from w w  w. java 2  s. c om*/
    return t;
}