Example usage for java.math BigDecimal compareTo

List of usage examples for java.math BigDecimal compareTo

Introduction

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

Prototype

@Override
public int compareTo(BigDecimal val) 

Source Link

Document

Compares this BigDecimal with the specified BigDecimal .

Usage

From source file:com.squarespace.template.plugins.platform.CommerceUtils.java

public static JsonNode getFromPriceMoneyNode(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch (type) {

    case PHYSICAL:
    case SERVICE:
    case GIFT_CARD:
        JsonNode variants = structuredContent.path("variants");
        if (variants.size() == 0) {
            return DEFAULT_MONEY_NODE;
        }/*w  w w .java  2s .  c  om*/

        JsonNode first = variants.get(0);
        JsonNode moneyNode = isTruthy(first.path("onSale")) ? first.path("salePriceMoney")
                : first.path("priceMoney");
        BigDecimal price = getAmountFromMoneyNode(moneyNode);
        for (int i = 1; i < variants.size(); i++) {
            JsonNode var = variants.get(i);
            JsonNode currentMoneyNode = isTruthy(var.path("onSale")) ? var.path("salePriceMoney")
                    : var.path("priceMoney");

            BigDecimal current = getAmountFromMoneyNode(currentMoneyNode);
            if (current.compareTo(price) < 0) {
                price = current;
                moneyNode = currentMoneyNode;
            }
        }
        return moneyNode;

    case DIGITAL:
        JsonNode digitalMoneyNode = structuredContent.path("priceMoney");
        return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;

    default:
        return DEFAULT_MONEY_NODE;
    }
}

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;/*ww  w  . j ava 2 s  .  c  om*/

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

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

    return easting;
}

From source file:Main.java

/**
 * Special compare method that gets around the problem in java 1.5,
 * where years and months are converted to days after arithmetic.
 * @param d1 First duration// ww  w  . j a  v  a 2 s  . co  m
 * @param d2 Second duration
 * @return -1 if d1 < d2, 0 if d1 == d2 and +1 if d1 > d2
 */
public static int compare(Duration d1, Duration d2) {
    if (d1 == null && d2 == null)
        return 0;

    if (d1 == null)
        return -1;

    if (d2 == null)
        return 1;

    boolean b1 = d1.getSign() >= 0;
    boolean b2 = d2.getSign() >= 0;

    if (!b1 && b2)
        return -1;

    if (b1 && !b2)
        return 1;

    // Now normalise in case we are running with java 1.5 runtime
    javax.xml.datatype.Duration n1 = normaliseDays(d1);
    javax.xml.datatype.Duration n2 = normaliseDays(d2);

    if (n1.getDays() < n2.getDays())
        return -1;
    if (n1.getDays() > n2.getDays())
        return 1;

    if (n1.getHours() < n2.getHours())
        return -1;
    if (n1.getHours() > n2.getHours())
        return 1;

    if (n1.getMinutes() < n2.getMinutes())
        return -1;
    if (n1.getMinutes() > n2.getMinutes())
        return 1;

    BigDecimal s1 = (BigDecimal) n1.getField(DatatypeConstants.SECONDS);
    BigDecimal s2 = (BigDecimal) n2.getField(DatatypeConstants.SECONDS);

    return s1.compareTo(s2);
}

From source file:Main.java

/**
 * Add two positive Duration objects./*www.  j a  v a  2s  .c o m*/
 * @param d1 The first Duration.
 * @param d2 The second Duration.
 * @return The sum of the two durations.
 */
private static Duration addPositiveDurations(Duration d1, Duration d2) {
    BigDecimal s1 = fractionalSeconds(d1);
    BigDecimal s2 = fractionalSeconds(d2);
    BigDecimal extraSeconds = s1.add(s2);

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

    Duration stripResult = strip1.add(strip2);

    if (extraSeconds.compareTo(BigDecimal.ONE) >= 0) {
        stripResult = stripResult.add(DURATION_1_SECOND);
        extraSeconds = extraSeconds.subtract(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: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/*from   w w  w .j a v a2s.  co  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:com.squarespace.template.plugins.platform.CommerceUtils.java

public static JsonNode getSalePriceMoneyNode(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch (type) {
    case PHYSICAL:
    case SERVICE:
        JsonNode variants = structuredContent.path("variants");
        if (variants.size() == 0) {
            return DEFAULT_MONEY_NODE;
        }//from w w  w  . ja v a2 s . c  o  m
        BigDecimal salePrice = null;
        JsonNode salePriceNode = null;
        for (int i = 0; i < variants.size(); i++) {
            JsonNode variant = variants.path(i);
            JsonNode priceMoney = variant.path("salePriceMoney");
            BigDecimal price = getAmountFromMoneyNode(priceMoney);
            if (isTruthy(variant.path("onSale")) && (salePriceNode == null || price.compareTo(salePrice) < 0)) {
                salePrice = price;
                salePriceNode = priceMoney;
            }
        }
        return (salePriceNode == null) ? DEFAULT_MONEY_NODE : salePriceNode;

    case DIGITAL:
        JsonNode digitalMoneyNode = structuredContent.path("salePriceMoney");
        return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;

    case GIFT_CARD:
        // this should never happen

    default:
        return DEFAULT_MONEY_NODE;
    }
}

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/*ww  w  .  j  ava 2  s  .c o  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;
}

From source file:com.sunchenbin.store.feilong.core.lang.NumberUtil.java

/**
 * Object  value,?./* w  ww.  ja v a  2  s  .  c o  m*/
 * 
 * <p>
 * value ?BigDecimal,specificNumber ?BigDecimal ,BigDecimal compareTo,<br>
 * 0 ,true.
 * </p>
 * 
 * @param value
 *            Object  value, Number  String
 * @param specificNumber
 *            
 * @return value ?BigDecimal,specificNumber ?BigDecimal ,BigDecimal compareTo,<br>
 *         0 ,true
 */
public static boolean isSpecificNumber(Serializable value, String specificNumber) {
    if (Validator.isNullOrEmpty(value)) {
        return false;
    }

    String valueString = value.toString();
    // Number /String
    if (value instanceof Number || value instanceof String) {
        BigDecimal bigDecimal = ConvertUtil.toBigDecimal(valueString);
        int i = bigDecimal.compareTo(ConvertUtil.toBigDecimal(specificNumber));
        return i == 0;
    }
    return false;
}

From source file:com.coinblesk.client.utils.UIUtils.java

public static boolean stringIsNotZero(String amountString) {
    //Checks if a string is actually of Zero value 0.00 0 0.000 etc.
    BigDecimal bd = new BigDecimal(amountString);
    return bd.compareTo(BigDecimal.ZERO) != 0;
}

From source file:jp.co.ctc_g.jse.core.validation.util.Validators.java

/**
 * ??????????????/*from w  w  w.j  ava  2 s.c om*/
 * @param suspect 
 * @param target ?
 * @return ???????false
 */
public static boolean greaterThan(Number suspect, BigDecimal target) {
    if (suspect == null)
        return true;
    BigDecimal number = toBigDecimal(suspect);
    return number.compareTo(target) >= 1;
}