java.math.BigDecimal.compareTo()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(415)

本文整理了Java中java.math.BigDecimal.compareTo()方法的一些代码示例,展示了BigDecimal.compareTo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigDecimal.compareTo()方法的具体详情如下:
包路径:java.math.BigDecimal
类名称:BigDecimal
方法名:compareTo

BigDecimal.compareTo介绍

[英]Compares this BigDecimal with the specified Object. If the Object is a BigDecimal, this method behaves like #compareTo. Otherwise, it throws a ClassCastException (as BigDecimals are comparable only to other BigDecimals).
[中]将此BigDecimal与指定的对象进行比较。如果对象是BigDecimal,则此方法的行为类似于#compareTo。否则,它抛出一个ClassCastException(因为大小数只能与其他大小数比较)。

代码示例

代码示例来源:origin: stackoverflow.com

if (new BigDecimal(someprice).compareTo(BigDecimal.ZERO) == 0) // see below

代码示例来源:origin: apache/incubator-druid

private static int compareDoubleToLong(final double a, final long b)
 {
  // Use BigDecimal when comparing integers vs floating points, a convenient way to handle all cases (like
  // fractional values, values out of range of max long/max int) without worrying about them ourselves.
  return BigDecimal.valueOf(a).compareTo(BigDecimal.valueOf(b));
 }
}

代码示例来源:origin: org.assertj/assertj-core

@Override
protected boolean isGreaterThan(BigDecimal value, BigDecimal other) {
 return value.subtract(other).compareTo(ZERO) > 0;
}

代码示例来源:origin: apache/hive

@Override
 public List<MutablePair<String, String>> getIntervals(String lowerBound, String upperBound, int numPartitions, TypeInfo
     typeInfo) {
  List<MutablePair<String, String>> intervals = new ArrayList<>();
  DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo)typeInfo;
  int scale = decimalTypeInfo.getScale();
  BigDecimal decimalLower = new BigDecimal(lowerBound);
  BigDecimal decimalUpper = new BigDecimal(upperBound);
  BigDecimal decimalInterval = (decimalUpper.subtract(decimalLower)).divide(new BigDecimal(numPartitions),
      MathContext.DECIMAL64);
  BigDecimal splitDecimalLower, splitDecimalUpper;
  for (int i=0;i<numPartitions;i++) {
   splitDecimalLower = decimalLower.add(decimalInterval.multiply(new BigDecimal(i))).setScale(scale,
       RoundingMode.HALF_EVEN);
   splitDecimalUpper = decimalLower.add(decimalInterval.multiply(new BigDecimal(i+1))).setScale(scale,
       RoundingMode.HALF_EVEN);
   if (splitDecimalLower.compareTo(splitDecimalUpper) < 0) {
    intervals.add(new MutablePair<String, String>(splitDecimalLower.toPlainString(), splitDecimalUpper.toPlainString()));
   }
  }
  return intervals;
 }
}

代码示例来源:origin: stackoverflow.com

BigDecimal step = new BigDecimal("0.1");
for (BigDecimal value = BigDecimal.ZERO;
   value.compareTo(BigDecimal.ONE) < 0;
   value = value.add(step)) {
  System.out.println(value);
}

代码示例来源:origin: org.codehaus.groovy/groovy

final BigDecimal one = BigDecimal.valueOf(10, 1);  // That's what you get for "1.0".
if (to instanceof BigDecimal) {
  BigDecimal to1 = (BigDecimal) to;
  if (self.compareTo(to1) <= 0) {
    for (BigDecimal i = self; i.compareTo(to1) <= 0; i = i.add(one)) {
      closure.call(i);
        ") to upto() cannot be less than the value (" + self + ") it's called on.");
} else if (to instanceof BigInteger) {
  BigDecimal to1 = new BigDecimal((BigInteger) to);
  if (self.compareTo(to1) <= 0) {
    for (BigDecimal i = self; i.compareTo(to1) <= 0; i = i.add(one)) {
      closure.call(i);
        ") to upto() cannot be less than the value (" + self + ") it's called on.");
} else {
  BigDecimal to1 = new BigDecimal(to.toString());
  if (self.compareTo(to1) <= 0) {
    for (BigDecimal i = self; i.compareTo(to1) <= 0; i = i.add(one)) {
      closure.call(i);

代码示例来源:origin: org.codehaus.groovy/groovy

final BigDecimal one = BigDecimal.valueOf(10, 1);  // Quick way to get "1.0".
if (to instanceof BigDecimal) {
  BigDecimal to1 = (BigDecimal) to;
  if (self.compareTo(to1) >= 0) {
    for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
      closure.call(i);
    throw new GroovyRuntimeException("The argument (" + to +
        ") to downto() cannot be greater than the value (" + self + ") it's called on.");        } else if (to instanceof BigInteger) {
  BigDecimal to1 = new BigDecimal((BigInteger) to);
  if (self.compareTo(to1) >= 0) {
    for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
      closure.call(i);
    throw new GroovyRuntimeException("The argument (" + to +
        ") to downto() cannot be greater than the value (" + self + ") it's called on.");        } else {
  BigDecimal to1 = new BigDecimal(to.toString());
  if (self.compareTo(to1) >= 0) {
    for (BigDecimal i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
      closure.call(i);

代码示例来源:origin: spring-projects/spring-framework

BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
return (leftBigDecimal.compareTo(rightBigDecimal) == 0);
BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
return (leftBigInteger.compareTo(rightBigInteger) == 0);

代码示例来源:origin: apache/hive

/**
 * Calculate difference between values in percent
 */
public BigDecimal percentDiff(Var var) {
 BigDecimal d1 = new Var(Var.Type.DECIMAL).cast(this).decimalValue();
 BigDecimal d2 = new Var(Var.Type.DECIMAL).cast(var).decimalValue();
 if (d1 != null && d2 != null) {
  if (d1.compareTo(BigDecimal.ZERO) != 0) {
   return d1.subtract(d2).abs().multiply(new BigDecimal(100)).divide(d1, 2, RoundingMode.HALF_UP);
  }
 }
 return null;
}

代码示例来源:origin: json-path/JsonPath

private static Number unwrapNumber(final Number n) {
  Number unwrapped;
  if (!isPrimitiveNumber(n)) {
    BigDecimal bigDecimal = new BigDecimal(n.toString());
    if (bigDecimal.scale() <= 0) {
      if (bigDecimal.compareTo(new BigDecimal(Integer.MAX_VALUE)) <= 0) {
        unwrapped = bigDecimal.intValue();
      } else if (bigDecimal.compareTo(new BigDecimal(Long.MAX_VALUE)) <= 0){
        unwrapped = bigDecimal.longValue();
      } else {
        unwrapped = bigDecimal;
      }
    } else {
      final double doubleValue = bigDecimal.doubleValue();
      if (BigDecimal.valueOf(doubleValue).compareTo(bigDecimal) != 0) {
        unwrapped = bigDecimal;
      } else {
        unwrapped = doubleValue;
      }
    }
  } else {
    unwrapped = n;
  }
  return unwrapped;
}

代码示例来源:origin: plantuml/plantuml

public BigDecimal eval(List<? extends Number> parameters) {
    assertNotNull(parameters.get(0));
    /*
     * From The Java Programmers Guide To numerical Computing (Ronald Mak, 2003)
     */
    BigDecimal x = (BigDecimal) parameters.get(0);
    if (x.compareTo(BigDecimal.ZERO) == 0) {
      return new BigDecimal(0);
    }
    if (x.signum() < 0) {
      throw new ExpressionException("Argument to SQRT() function must not be negative");
    }
    BigInteger n = x.movePointRight(mc.getPrecision() << 1).toBigInteger();
    int bits = (n.bitLength() + 1) >> 1;
    BigInteger ix = n.shiftRight(bits);
    BigInteger ixPrev;
    BigInteger test;
    do {
      ixPrev = ix;
      ix = ix.add(n.divide(ix)).shiftRight(1);
      // Give other threads a chance to work;
      Thread.yield();
      test = ix.subtract(ixPrev).abs();
    } while (test.compareTo(BigInteger.ZERO) != 0 && test.compareTo(BigInteger.ONE) != 0);
    return new BigDecimal(ix, mc.getPrecision());
  }
});

代码示例来源:origin: stackoverflow.com

return BigDecimal.valueOf(magnitude).multiply(lnRoot)
        .setScale(scale, BigDecimal.ROUND_HALF_EVEN);
BigDecimal tolerance = BigDecimal.valueOf(5)
                  .movePointLeft(sp1);
  term = eToX.subtract(n)
        .divide(eToX, sp1, BigDecimal.ROUND_DOWN);
  x = x.subtract(term);
} while (term.compareTo(tolerance) > 0);

代码示例来源:origin: openhab/openhab1-addons

/**
 * Canonicalize the current latitude and longitude values such that:
 *
 * <pre>
 * -90 &lt;= latitude &lt;= +90 - 180 &lt; longitude &lt;= +180
 * </pre>
 */
private void canonicalize(DecimalType aLat, DecimalType aLon) {
  latitude = flat.add(aLat.toBigDecimal()).remainder(circle);
  longitude = aLon.toBigDecimal();
  if (latitude.compareTo(BigDecimal.ZERO) == -1) {
    latitude.add(circle);
  }
  latitude = latitude.subtract(flat);
  if (latitude.compareTo(right) == 1) {
    latitude = flat.subtract(latitude);
    longitude = longitude.add(flat);
  } else if (latitude.compareTo(right.negate()) == -1) {
    latitude = flat.negate().subtract(latitude);
    longitude = longitude.add(flat);
  }
  longitude = flat.add(longitude).remainder(circle);
  if (longitude.compareTo(BigDecimal.ZERO) <= 0) {
    longitude = longitude.add(circle);
  }
  longitude = longitude.subtract(flat);
}

代码示例来源:origin: debezium/debezium

/**
   * Convert original value insertion of type 'BIGINT' into the correct BIGINT UNSIGNED representation
   * Note: Unsigned BIGINT (16-bit) is represented in 'BigDecimal' data type. Reference: https://kafka.apache.org/0102/javadoc/org/apache/kafka/connect/data/Schema.Type.html
   *
   * @param originalNumber {@link BigDecimal} the original insertion value
   * @return {@link BigDecimal} the correct representation of the original insertion value
   */
  public static BigDecimal convertUnsignedBigint(BigDecimal originalNumber) {
    if (originalNumber.compareTo(BigDecimal.ZERO) == -1) {
      return originalNumber.add(BIGINT_CORRECTION);
    } else {
      return originalNumber;
    }
  }
}

代码示例来源:origin: jphp-group/jphp

private static BigDecimal bcpowImpl(BigDecimal base, BigInteger exp, int scale) {
  if (exp.compareTo(BigInteger.ZERO) == 0)
    return BigDecimal.ONE;
  boolean isNeg;
  if (exp.compareTo(BigInteger.ZERO) < 0) {
    isNeg = true;
    exp = exp.negate();
  }
  else
    isNeg = false;
  BigDecimal result = BigDecimal.ZERO;
  while (exp.compareTo(BigInteger.ZERO) > 0) {
    BigInteger expSub = exp.min(INTEGER_MAX);
    exp = exp.subtract(expSub);
    result = result.add(base.pow(expSub.intValue()));
  }
  if (isNeg)
    result = BigDecimal.ONE.divide(result, scale + 2, RoundingMode.DOWN);
  result = result.setScale(scale, RoundingMode.DOWN);
  if (result.compareTo(BigDecimal.ZERO) == 0)
    return BigDecimal.ZERO;
  result = result.stripTrailingZeros();
  return result;
}

代码示例来源:origin: openhab/openhab1-addons

private void validateValue(BigDecimal value) {
  if (BigDecimal.ZERO.compareTo(value) > 0 || new BigDecimal(100).compareTo(value) < 0) {
    throw new IllegalArgumentException("Value must be between 0 and 100");
  }
}

代码示例来源:origin: knowm/XChange

public static LimitOrder adaptOrder(CexIOFullOrder cexIOOrder) {
 OrderType orderType = cexIOOrder.type.equals("sell") ? OrderType.ASK : OrderType.BID;
 BigDecimal originalAmount = new BigDecimal(cexIOOrder.amount);
 CurrencyPair currencyPair = new CurrencyPair(cexIOOrder.symbol1, cexIOOrder.symbol2);
 Date timestamp = new Date(cexIOOrder.time);
 BigDecimal limitPrice = new BigDecimal(cexIOOrder.price);
 Order.OrderStatus status = adaptOrderStatus(cexIOOrder);
 BigDecimal cumulativeAmount = null;
  BigDecimal remains = new BigDecimal(cexIOOrder.remains);
  cumulativeAmount = originalAmount.subtract(remains);
     ? new BigDecimal(cexIOOrder.totalAmountTaker)
     : BigDecimal.ZERO;
 BigDecimal tradedAmount = totalAmountMaker.add(totalAmountTaker);
 if (cumulativeAmount != null && tradedAmount.compareTo(BigDecimal.ZERO) > 0) {
  averagePrice = tradedAmount.divide(cumulativeAmount, 2, RoundingMode.HALF_UP);
 BigDecimal feeTaker =
   cexIOOrder.feeTaker != null ? new BigDecimal(cexIOOrder.feeTaker) : BigDecimal.ZERO;
 BigDecimal fee = feeMaker.add(feeTaker);
 return new LimitOrder(
   orderType,
   averagePrice,
   cumulativeAmount,
   fee.compareTo(BigDecimal.ZERO) > 0 ? fee : null,
   status);

代码示例来源:origin: deeplearning4j/nd4j

if (x.compareTo(BigDecimal.ZERO) < 0) {
  return cos(x.negate());
} else if (x.compareTo(BigDecimal.ZERO) == 0) {
  return BigDecimal.ONE;
} else {
      ifac = ifac.multiply(BigInteger.valueOf(2 * i));
      xpowi = xpowi.multiply(xhighpr).multiply(xhighpr);
      BigDecimal corr = xpowi.divide(new BigDecimal(ifac), mcTay);
      resul = resul.add(corr);

代码示例来源:origin: org.codehaus.groovy/groovy

final BigDecimal one = BigDecimal.valueOf(10, 1);
BigDecimal self1 = new BigDecimal(self);
BigDecimal to1 = (BigDecimal) to;
if (self1.compareTo(to1) <= 0) {
  for (BigDecimal i = self1; i.compareTo(to1) <= 0; i = i.add(one)) {
    closure.call(i);
final BigInteger one = BigInteger.valueOf(1);
BigInteger to1 = (BigInteger) to;
if (self.compareTo(to1) <= 0) {
  for (BigInteger i = self; i.compareTo(to1) <= 0; i = i.add(one)) {
    closure.call(i);
final BigInteger one = BigInteger.valueOf(1);
BigInteger to1 = new BigInteger(to.toString());
if (self.compareTo(to1) <= 0) {
  for (BigInteger i = self; i.compareTo(to1) <= 0; i = i.add(one)) {
    closure.call(i);

代码示例来源:origin: org.codehaus.groovy/groovy

final BigDecimal one = BigDecimal.valueOf(10, 1);  // That's what you get for "1.0".
final BigDecimal to1 = (BigDecimal) to;
final BigDecimal selfD = new BigDecimal(self);
if (selfD.compareTo(to1) >= 0) {
  for (BigDecimal i = selfD; i.compareTo(to1) >= 0; i = i.subtract(one)) {
    closure.call(i.toBigInteger());
final BigInteger one = BigInteger.valueOf(1);
final BigInteger to1 = (BigInteger) to;
if (self.compareTo(to1) >= 0) {
  for (BigInteger i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
    closure.call(i);
final BigInteger one = BigInteger.valueOf(1);
final BigInteger to1 = new BigInteger(to.toString());
if (self.compareTo(to1) >= 0) {
  for (BigInteger i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
    closure.call(i);

相关文章