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.aoindustries.website.clientarea.accounting.MakePaymentNewCardForm.java

@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errors = super.validate(mapping, request);
    if (errors == null)
        errors = new ActionErrors();
    if (GenericValidator.isBlankOrNull(paymentAmount)) {
        errors.add("paymentAmount", new ActionMessage("makePaymentStoredCardForm.paymentAmount.required"));
    } else {/*w w  w  .ja  v a2s .c  o m*/
        try {
            // Make sure can parse as int-of-pennies format (Once we no longer use int-of-pennies, this should be removed)
            // Long-term plan is to use BigDecimal exclusively for all monetary values. - DRA 2007-10-09
            int pennies = SQLUtility.getPennies(this.paymentAmount);
            // Make sure can parse as BigDecimal, and is correct value
            BigDecimal pa = new BigDecimal(this.paymentAmount);
            if (pa.compareTo(BigDecimal.ZERO) <= 0) {
                errors.add("paymentAmount",
                        new ActionMessage("makePaymentStoredCardForm.paymentAmount.mustBeGeaterThanZero"));
            } else if (pa.scale() > 2) {
                // Must not have more than 2 decimal places
                errors.add("paymentAmount",
                        new ActionMessage("makePaymentStoredCardForm.paymentAmount.invalid"));
            }
        } catch (NumberFormatException err) {
            errors.add("paymentAmount", new ActionMessage("makePaymentStoredCardForm.paymentAmount.invalid"));
        }
    }
    return errors;
}

From source file:com.intuit.karate.Script.java

private static AssertionResult matchPrimitive(String path, Object actObject, Object expObject) {
    if (actObject == null) {
        return matchFailed(path, actObject, expObject, "actual value is null");
    }//from   w ww  .  j a v a2s  .c  o  m
    if (!expObject.getClass().equals(actObject.getClass())) {
        if (actObject instanceof BigDecimal) {
            BigDecimal actNumber = (BigDecimal) actObject;
            BigDecimal expNumber = convertToBigDecimal(expObject);
            if (expNumber == null || expNumber.compareTo(actNumber) != 0) {
                return matchFailed(path, actObject, expObject, "not equal (big decimal : primitive)");
            } else {
                return AssertionResult.PASS;
            }
        } else {
            // types are not the same, use the JS engine for a lenient equality check
            String exp = actObject + " == " + expObject;
            ScriptValue sv = evalInNashorn(exp, null);
            if (sv.isBooleanTrue()) {
                return AssertionResult.PASS;
            } else {
                return matchFailed(path, actObject, expObject, "not equal");
            }
        }
    }
    if (!expObject.equals(actObject)) {
        return matchFailed(path, actObject, expObject, "not equal");
    } else {
        return AssertionResult.PASS; // primitives, are equal
    }
}

From source file:fuzzy.util.MaxMF.java

public Map<Double, Double> evaluate(Collection<T> x, MembershipFunction<T> mf) {
    Map<Double, Double> max = new HashMap<Double, Double>();
    BigDecimal maxValue = BigDecimal.valueOf(0.0);
    boolean first = true;
    for (T value : x) {
        BigDecimal temp = new BigDecimal(mf.evaluate(value), new MathContext(precision, roundingMode));
        if (first || temp.compareTo(maxValue) > 0) {
            first = false;/*from  w ww  .  jav a2 s.  co  m*/
            maxValue = temp;
            max.clear();
            max.put(value.doubleValue(), temp.doubleValue());
        } else if (temp.compareTo(maxValue) == 0) {
            max.put(value.doubleValue(), temp.doubleValue());
        } // else ignore since it's less than the maximum value
    }
    return max;
}

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

public void set(BigDecimal value) {
    value = value.stripTrailingZeros();// w  w w  .j  a v  a 2s.  c  o  m
    if (value.compareTo(BigDecimal.ZERO) == 0) {
        // Special case for 0, because java doesn't strip zeros correctly on that number.
        value = BigDecimal.ZERO;
    }
    set(value.unscaledValue().toByteArray(), value.scale());
}

From source file:io.curly.advisor.model.Review.java

private BigDecimal fixPrecision(BigDecimal rate) {
    if (rate == null)
        return BigDecimal.ZERO;
    BigDecimal decimal = rate.remainder(BigDecimal.ONE);
    BigInteger integer = rate.toBigInteger();
    if (decimal.compareTo(new BigDecimal(0.5)) > 0) {
        return new BigDecimal(integer.add(new BigInteger("1")));
    } else if (decimal.compareTo(new BigDecimal(0.5)) < 0) {
        return new BigDecimal(integer);
    }/* w w  w  .ja  v a  2  s  . c om*/
    return rate;
}

From source file:io.curly.advisor.model.Review.java

/**
 * Function to round up when the decimal part is more than a half, and down when its less than, if it is a half keep it
 *
 * @return fixed rate/*from   w  ww. j  ava  2s  . c  om*/
 */
public BigDecimal fixPrecision() {
    if (rate == null)
        return null;
    BigDecimal decimal = rate.remainder(BigDecimal.ONE);
    BigInteger integer = rate.toBigInteger();
    if (decimal.compareTo(new BigDecimal(0.5)) > 0) {
        rate = new BigDecimal(integer.add(new BigInteger("1")));
    } else if (decimal.compareTo(new BigDecimal(0.5)) < 0) {
        rate = new BigDecimal(integer);
    }
    return rate;
}

From source file:it.govpay.core.utils.RtUtils.java

private static void validaSemantica(CtDatiVersamentoRPT rpt, CtDatiVersamentoRT rt, EsitoValidazione esito) {

    valida(rpt.getCodiceContestoPagamento(), rt.getCodiceContestoPagamento(), esito,
            "CodiceContestoPagamento non corrisponde", true);
    valida(rpt.getIdentificativoUnivocoVersamento(), rt.getIdentificativoUnivocoVersamento(), esito,
            "IdentificativoUnivocoVersamento non corrisponde", true);

    Rpt.EsitoPagamento esitoPagamento = validaSemanticaCodiceEsitoPagamento(rt.getCodiceEsitoPagamento(),
            esito);//from   w w  w.j av  a2 s .co  m

    // Se siamo in pagamento eseguito, parzialmente eseguito o parzialmente decorso, devono esserci tanti versamenti quanti pagamenti.
    if (esitoPagamento != null) {
        switch (esitoPagamento) {
        case DECORRENZA_TERMINI_PARZIALE:
        case PAGAMENTO_ESEGUITO:
        case PAGAMENTO_PARZIALMENTE_ESEGUITO:
            if (rt.getDatiSingoloPagamento().size() != rpt.getDatiSingoloVersamento().size()) {
                esito.addErrore("Numero di pagamenti diverso dal numero di versamenti per una ricevuta di tipo "
                        + esitoPagamento.name(), true);
                return;
            }
        case DECORRENZA_TERMINI:
        case PAGAMENTO_NON_ESEGUITO:
            if (rt.getDatiSingoloPagamento().size() != 0
                    && rt.getDatiSingoloPagamento().size() != rpt.getDatiSingoloVersamento().size()) {
                esito.addErrore("Numero di pagamenti diverso dal numero di versamenti per una ricevuta di tipo "
                        + esitoPagamento.name(), true);
                return;
            }
        }
    }

    BigDecimal importoTotaleCalcolato = BigDecimal.ZERO;

    for (int i = 0; i < rpt.getDatiSingoloVersamento().size(); i++) {

        CtDatiSingoloVersamentoRPT singoloVersamento = rpt.getDatiSingoloVersamento().get(i);
        CtDatiSingoloPagamentoRT singoloPagamento = null;
        if (rt.getDatiSingoloPagamento().size() != 0) {
            singoloPagamento = rt.getDatiSingoloPagamento().get(i);
            validaSemanticaSingoloVersamento(singoloVersamento, singoloPagamento, esito);
            importoTotaleCalcolato = importoTotaleCalcolato.add(singoloPagamento.getSingoloImportoPagato());
        }
    }

    if (importoTotaleCalcolato.compareTo(rt.getImportoTotalePagato()) != 0)
        esito.addErrore("ImportoTotalePagato [" + rt.getImportoTotalePagato().doubleValue()
                + "] non corrisponde alla somma dei SingoliImportiPagati ["
                + importoTotaleCalcolato.doubleValue() + "]", true);
    if (esitoPagamento == Rpt.EsitoPagamento.PAGAMENTO_NON_ESEGUITO
            && rt.getImportoTotalePagato().compareTo(BigDecimal.ZERO) != 0)
        esito.addErrore("ImportoTotalePagato [" + rt.getImportoTotalePagato().doubleValue()
                + "] diverso da 0 per un pagamento con esito 'Non Eseguito'.", true);
    if (esitoPagamento == Rpt.EsitoPagamento.DECORRENZA_TERMINI
            && rt.getImportoTotalePagato().compareTo(BigDecimal.ZERO) != 0)
        esito.addErrore("ImportoTotalePagato [" + rt.getImportoTotalePagato().doubleValue()
                + "] diverso da 0 per un pagamento con esito 'Decorrenza temini'.", true);
    if (esitoPagamento == Rpt.EsitoPagamento.PAGAMENTO_ESEGUITO
            && rt.getImportoTotalePagato().compareTo(rpt.getImportoTotaleDaVersare()) != 0)
        esito.addErrore("Importo totale del pagamento [" + rt.getImportoTotalePagato().doubleValue()
                + "] diverso da quanto richiesto [" + rpt.getImportoTotaleDaVersare().doubleValue() + "]",
                false);
}

From source file:org.whispersystems.bithub.controllers.GithubController.java

private boolean isViablePaymentAmount(BigDecimal payment) {
    return payment.compareTo(new BigDecimal(0)) == 1;
}

From source file:com.willetinc.hadoop.mapreduce.dynamodb.BigDecimalSplitter.java

/**
 * <p>/*from   w  w w .  j ava 2 s . c  om*/
 * Returns a list of BigDecimals one element longer than the list of input
 * splits. This represents the boundaries between input splits. All splits
 * are open on the top end, except the last one.
 * </p>
 * 
 * <p>
 * So the list [0, 5, 8, 12, 18] would represent splits capturing the
 * intervals:
 * </p>
 * 
 * <p>
 * The smallest positive value supported by DynamoDB 'e' is used to separate
 * intervals
 * </p>
 * 
 * <p>
 * e = 0.0000000000000000000000000000000000001
 * </p>
 * 
 * <p>
 * [0, 5] [5+e, 8] [8+e, 12] [12+e, 18]
 * </p>
 */
List<BigDecimal> split(BigDecimal numSplits, BigDecimal minVal, BigDecimal maxVal) {

    List<BigDecimal> splits = new ArrayList<BigDecimal>();

    // Use numSplits as a hint. May need an extra task if the size doesn't
    // divide cleanly.

    BigDecimal splitSize = tryDivide(maxVal.subtract(minVal), (numSplits));
    if (splitSize.compareTo(MIN_INCREMENT) < 0) {
        splitSize = MIN_INCREMENT;
        LOG.warn("Set BigDecimal splitSize to MIN_INCREMENT");
    }

    BigDecimal curVal = minVal;

    while (curVal.compareTo(maxVal) <= 0) {
        splits.add(curVal);
        curVal = curVal.add(splitSize);
    }

    if (splits.get(splits.size() - 1).compareTo(maxVal) != 0 || splits.size() == 1) {
        // We didn't end on the maxVal. Add that to the end of the list.
        splits.add(maxVal);
    }

    return splits;
}

From source file:org.apache.tajo.validation.MaxValidator.java

@Override
protected <T> boolean validateInternal(T object) {
    boolean result = false;

    if (object != null) {
        if ((object instanceof Byte) || (object instanceof Short) || (object instanceof Integer)) {
            Integer objInteger = Integer.decode(object.toString());
            Integer maxInteger = Integer.decode(maxValue);
            result = objInteger.compareTo(maxInteger) <= 0;
        } else if (object instanceof Long) {
            Long objLong = Long.decode(object.toString());
            Long maxLong = Long.decode(maxValue);
            result = objLong.compareTo(maxLong) <= 0;
        } else if ((object instanceof Float) || (object instanceof Double)) {
            Double objDouble = Double.valueOf(object.toString());
            Double maxDouble = Double.valueOf(maxValue);
            result = objDouble.compareTo(maxDouble) <= 0;
        } else if (object instanceof BigInteger) {
            BigInteger objInteger = (BigInteger) object;
            BigInteger maxInteger = new BigInteger(maxValue);
            result = objInteger.compareTo(maxInteger) <= 0;
        } else if (object instanceof BigDecimal) {
            BigDecimal objDecimal = (BigDecimal) object;
            BigDecimal maxDecimal = new BigDecimal(maxValue);
            result = objDecimal.compareTo(maxDecimal) <= 0;
        }//from   www  .  ja  v a2  s .  c om
    } else {
        result = true;
    }

    return result;
}