List of usage examples for java.math BigDecimal compareTo
@Override public int compareTo(BigDecimal val)
From source file:to.sparks.mtgox.example.TradingBot.java
private static boolean isDiffTooLarge(BigDecimal actualPrice, BigDecimal optimumPrice) { BigDecimal diff;//ww w. j ava 2 s . c om if (actualPrice.compareTo(optimumPrice) < 0) { diff = optimumPrice.subtract(actualPrice); } else { diff = actualPrice.subtract(optimumPrice); } return diff.compareTo(optimumPrice.multiply(percentAllowedPriceDeviation)) > 0; }
From source file:org.cirdles.calamari.algorithms.TukeyBiweight.java
public static ValueModel calculateTukeyBiweightMean(String name, double tuningConstant, double[] values) { // guarantee termination BigDecimal epsilon = BigDecimal.ONE.movePointLeft(10); int iterationMax = 100; int iterationCounter = 0; int n = values.length; // initial mean is median BigDecimal mean = new BigDecimal(calculateMedian(values)); // initial sigma is median absolute deviation from mean = median (MAD) double deviations[] = new double[n]; for (int i = 0; i < values.length; i++) { deviations[i] = StrictMath.abs(values[i] - mean.doubleValue()); }/* w ww.j av a 2s . c o m*/ BigDecimal sigma = new BigDecimal(calculateMedian(deviations)).max(BigDecimal.valueOf(SQUID_TINY_VALUE)); BigDecimal previousMean; BigDecimal previousSigma; do { iterationCounter++; previousMean = mean; previousSigma = sigma; // init to zeroes BigDecimal[] deltas = new BigDecimal[n]; BigDecimal[] u = new BigDecimal[n]; BigDecimal sa = BigDecimal.ZERO; BigDecimal sb = BigDecimal.ZERO; BigDecimal sc = BigDecimal.ZERO; BigDecimal tee = new BigDecimal(tuningConstant).multiply(sigma); for (int i = 0; i < n; i++) { deltas[i] = new BigDecimal(values[i]).subtract(mean); if (tee.compareTo(deltas[i].abs()) > 0) { deltas[i] = new BigDecimal(values[i]).subtract(mean); u[i] = deltas[i].divide(tee, MathContext.DECIMAL128); BigDecimal uSquared = u[i].multiply(u[i]); sa = sa.add(deltas[i].multiply(BigDecimal.ONE.subtract(uSquared).pow(2)).pow(2)); sb = sb.add(BigDecimal.ONE.subtract(uSquared) .multiply(BigDecimal.ONE.subtract(new BigDecimal(5.0).multiply(uSquared)))); sc = sc.add(u[i].multiply(BigDecimal.ONE.subtract(uSquared).pow(2))); } } sigma = bigDecimalSqrtBabylonian(sa.multiply(new BigDecimal(n))).divide(sb.abs(), MathContext.DECIMAL128); sigma = sigma.max(BigDecimal.valueOf(SQUID_TINY_VALUE)); mean = previousMean.add(tee.multiply(sc).divide(sb, MathContext.DECIMAL128)); } // both tests against epsilon must pass OR iterations top out // april 2016 Simon B discovered we need 101 iterations possible, hence the "<=" below while (((sigma.subtract(previousSigma).abs().divide(sigma, MathContext.DECIMAL128).compareTo(epsilon) > 0)// || mean.subtract(previousMean).abs().divide(mean, MathContext.DECIMAL128).compareTo(epsilon) > 0)// && (iterationCounter <= iterationMax)); return new ValueModel(name, mean, "ABS", sigma); }
From source file:com.skubit.iab.activities.TransactionDetailsActivity.java
private static final String formatCurrencyAmount(BigDecimal balanceNumber) { NumberFormat numberFormat = NumberFormat.getInstance(Locale.getDefault()); numberFormat.setMaximumFractionDigits(8); numberFormat.setMinimumFractionDigits(4); if (balanceNumber.compareTo(BigDecimal.ZERO) == -1) { balanceNumber = balanceNumber.multiply(new BigDecimal(-1)); }//w ww.ja va 2 s. c o m return numberFormat.format(balanceNumber); }
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;//from w w w . jav a2s .c o m 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:och.comp.ops.BillingOps.java
public static boolean isNeedDeblockAccsState(BigDecimal oldBalance, BigDecimal newBalance, BigDecimal minActiveBalance) { if (oldBalance.compareTo(minActiveBalance) >= 0) return false; return newBalance.compareTo(minActiveBalance) >= 0; }
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 ww .j av a 2s . c o m return savingsNow.subtract(savingsThen).divide(avgIncome, CONTEXT); }
From source file:Main.java
/** * Write a {@link BigDecimal} value into XML output. * * @param value//from w ww . j ava 2 s . co m * value to write * * @param min * minimal value * * @param max * maximal value * * @param precision * number of decimal places * * @return * XML string * * @throws IllegalArgumentException * if a validation error occured */ private static String printDecimal(BigDecimal value, BigDecimal min, BigDecimal max, int precision, boolean zeroIncluded) { if (value == null) { throw new IllegalArgumentException("The provided double value NULL is invalid!"); } value = value.setScale(precision, BigDecimal.ROUND_HALF_UP); if (min != null && value.compareTo(min) <= 0) { if (!zeroIncluded || !BigDecimal.ZERO.equals(value)) { throw new IllegalArgumentException( "The provided double value " + value + " is too small (minimum is " + min + ")!"); } } if (max != null && value.compareTo(max) >= 0) { throw new IllegalArgumentException( "The provided double value " + value + " is too high (maximum is " + max + ")!"); } return DatatypeConverter.printDecimal(value); }
From source file:BigDSqrt.java
public static BigDecimal sqrt(BigDecimal n, int s) { BigDecimal TWO = BigDecimal.valueOf(2); // Obtain the first approximation BigDecimal x = n.divide(BigDecimal.valueOf(3), s, BigDecimal.ROUND_DOWN); BigDecimal lastX = BigDecimal.valueOf(0); // Proceed through 50 iterations for (int i = 0; i < 50; i++) { x = n.add(x.multiply(x)).divide(x.multiply(TWO), s, BigDecimal.ROUND_DOWN); if (x.compareTo(lastX) == 0) break; lastX = x;//from ww w .j a v a2s . c o m } return x; }
From source file:uk.dsxt.voting.tests.TestDataGenerator.java
private static VoteResult generateVote(String id, HashMap<String, BigDecimal> securities, Voting voting) { VoteResult vote = new VoteResult(voting.getId(), id, securities.get(SECURITY)); for (int j = 0; j < voting.getQuestions().length; j++) { String questionId = voting.getQuestions()[j].getId(); if (voting.getQuestions()[j].isCanSelectMultiple()) { BigDecimal totalSum = BigDecimal.ZERO; for (int i = 0; i < voting.getQuestions()[j].getAnswers().length; i++) { String answerId = voting.getQuestions()[j].getAnswers()[i].getId(); int amount = randomInt(0, vote.getPacketSize().subtract(totalSum).intValue()); BigDecimal voteAmount = new BigDecimal(amount); totalSum = totalSum.add(voteAmount); if (voteAmount.compareTo(BigDecimal.ZERO) > 0) vote.setAnswer(questionId, answerId, voteAmount); }//from ww w . j a v a 2 s. c om } else { String answerId = voting.getQuestions()[j].getAnswers()[randomInt(0, voting.getQuestions()[j].getAnswers().length - 1)].getId(); BigDecimal voteAmount = new BigDecimal(randomInt(0, vote.getPacketSize().intValue())); if (voteAmount.compareTo(BigDecimal.ZERO) > 0) vote.setAnswer(questionId, answerId, voteAmount); } } return vote; }
From source file:com.squarespace.template.plugins.platform.CommerceUtils.java
public static JsonNode getNormalPriceMoneyNode(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 . jav a 2 s. co m JsonNode moneyNode = variants.get(0).path("priceMoney"); BigDecimal price = getAmountFromMoneyNode(moneyNode); for (int i = 1; i < variants.size(); i++) { JsonNode currMoneyNode = variants.get(i).path("priceMoney"); BigDecimal curr = getAmountFromMoneyNode(currMoneyNode); if (curr.compareTo(price) > 0) { price = curr; moneyNode = currMoneyNode; } } return moneyNode; case DIGITAL: JsonNode digitalMoneyNode = structuredContent.path("priceMoney"); return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode; default: return DEFAULT_MONEY_NODE; } }