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

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

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

BigDecimal.divide介绍

[英]Returns a new BigDecimal whose value is this / divisor. The scale of the result is the difference of the scales of thisand divisor. If the exact result requires more digits, then the scale is adjusted accordingly. For example, 1/128 = 0.0078125which has a scale of 7 and precision 5.
[中]返回一个新的BigDecimal,其值为this/除数。结果的尺度是这个和除数的尺度之差。如果精确结果需要更多数字,则相应地调整刻度。例如,1/128=0.0078125,其刻度为7,精度为5。

代码示例

代码示例来源:origin: vipshop/vjtools

/**
 * 人民币金额单位转换,分转换成元,取两位小数 例如:150 => 1.5
 */
public static BigDecimal fen2yuan(BigDecimal num) {
  return num.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
}

代码示例来源:origin: cucumber/cucumber-jvm

private BigDecimal toSeconds(Long nanoSeconds) {
  return BigDecimal.valueOf(nanoSeconds).divide(NANOS_PER_SECOND);
}

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

public BigDecimal getAveragePrice() {
 return low.add(high).divide(new BigDecimal("2"));
}

代码示例来源: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: knowm/XChange

public BigDecimal calculateFeeBtc() {
 return roundUp(amount.multiply(new BigDecimal(.5))).divide(new BigDecimal(100.));
}

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

@Override
public synchronized long getAverageSessionAliveTime() {
  //this method needs to be synchronised to make sure the session count and the total are in sync
  if(expiredSessionCount == 0) {
    return 0;
  }
  return new BigDecimal(totalSessionLifetime).divide(BigDecimal.valueOf(expiredSessionCount), MathContext.DECIMAL128).longValue();
}

代码示例来源:origin: org.kill-bill.billing/killbill-invoice

@Test(groups = "fast")
public void testDoubleProRation_TargetDateOnSecondBillingCycleDate() throws InvalidDateSequenceException {
  final LocalDate startDate = invoiceUtil.buildDate(2011, 1, 1);
  final LocalDate targetDate = invoiceUtil.buildDate(2012, 1, 15);
  final LocalDate endDate = invoiceUtil.buildDate(2012, 1, 27);
  BigDecimal expectedValue;
  expectedValue = FOURTEEN.divide(THREE_HUNDRED_AND_SIXTY_FIVE, KillBillMoney.ROUNDING_METHOD);
  expectedValue = expectedValue.add(ONE);
  expectedValue = expectedValue.add(TWELVE.divide(THREE_HUNDRED_AND_SIXTY_SIX, KillBillMoney.ROUNDING_METHOD));
  testCalculateNumberOfBillingCycles(startDate, endDate, targetDate, 15, expectedValue);
}

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

private PercentType byteToPercentType(int byteValue) {
  BigDecimal percentValue = new BigDecimal(byteValue).multiply(BigDecimal.valueOf(100))
      .divide(BigDecimal.valueOf(255), 2, BigDecimal.ROUND_HALF_UP);
  return new PercentType(percentValue);
}

代码示例来源:origin: prestodb/presto

@VisibleForTesting
public static BigDecimal average(LongDecimalWithOverflowAndLongState state, DecimalType type)
{
  BigDecimal sum = new BigDecimal(Decimals.decodeUnscaledValue(state.getLongDecimal()), type.getScale());
  BigDecimal count = BigDecimal.valueOf(state.getLong());
  if (state.getOverflow() != 0) {
    BigInteger overflowMultiplier = TWO.shiftLeft(UNSCALED_DECIMAL_128_SLICE_LENGTH * 8 - 2);
    BigInteger overflow = overflowMultiplier.multiply(BigInteger.valueOf(state.getOverflow()));
    sum = sum.add(new BigDecimal(overflow));
  }
  return sum.divide(count, type.getScale(), ROUND_HALF_UP);
}

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

/**
 * <p>
 *   Returns the average of all the numbers contained in the provided array.
 * </p>
 * 
 * @param target the array of numbers
 * @return the average, as a BigDecimal
 */
public static BigDecimal avg(final long[] target) {
  Validate.notNull(target, "Cannot aggregate on null");
  if (target.length == 0) {
    return null;
  }
  BigDecimal total = BigDecimal.ZERO;
  for (final long element : target) {
    total = total.add(toBigDecimal(element));
  }
  final BigDecimal divisor = BigDecimal.valueOf(target.length);
  try {
    return total.divide(divisor);
  } catch (final ArithmeticException e) {
    // We will get an arithmetic exception if: 1. Divisor is zero, which is impossible; or 2. Division
    // returns a number with a non-terminating decimal expansion. In the latter case, we will set the
    // scale manually.
    return total.divide(divisor, Math.max(total.scale(), 10), RoundingMode.HALF_UP);
  }
}

代码示例来源: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);

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

/**
 * get the value which is converted to specified unit.
 *
 * @param unit size unit
 * @return the converted value
 */
public double get(Unit unit) {
 if (value == 0) {
  return value;
 }
 int diff = this.unit.getOrderOfSize() - unit.getOrderOfSize();
 if (diff == 0) {
  return value;
 }
 BigDecimal rval = BigDecimal.valueOf(value);
 for (int i = 0; i != Math.abs(diff); ++i) {
  rval = diff > 0 ? rval.multiply(SCALE_BASE) : rval.divide(SCALE_BASE);
 }
 return rval.doubleValue();
}

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

/**
 * The raw transfer rate is represented as an integer, the amount that must be sent in order for 1
 * billion units to arrive. For example, a 20% transfer fee is represented as the value 120000000.
 * The value cannot be less than 1000000000. Less than that would indicate giving away money for
 * sending transactions, which is exploitable. You can specify 0 as a shortcut for 1000000000,
 * meaning no fee.
 *
 * @return percentage transfer rate charge
 */
public BigDecimal getTransferFeeRate() {
 if (transferRate == 0) {
  return BigDecimal.ZERO;
 } else {
  return BigDecimal.valueOf(transferRate)
    .divide(TRANSFER_RATE_DENOMINATOR)
    .subtract(BigDecimal.ONE);
 }
}

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

totalValue = totalValue.add(trade.getCurrency1Amount().multiply(trade.getRate()));
totalQuantity = totalQuantity.add(trade.getCurrency1Amount());
totalFee = totalFee.add(trade.getCommissionPaid());
 totalValue.divide(totalQuantity, 8, BigDecimal.ROUND_HALF_UP);

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

public static BigDecimal round(BigDecimal value, BigDecimal increment,
                RoundingMode roundingMode) {
  if (increment.signum() == 0) {
    // 0 increment does not make much sense, but prevent division by 0
    return value;
  } else {
    BigDecimal divided = value.divide(increment, 0, roundingMode);
    BigDecimal result = divided.multiply(increment);
    return result;
  }
}

代码示例来源:origin: macrozheng/mall

cartPromotionItem.setReduceAmount(originalPrice.subtract(skuStock.getPromotionPrice()));
cartPromotionItem.setRealStock(skuStock.getStock()-skuStock.getLockStock());
cartPromotionItem.setIntegration(promotionProduct.getGiftPoint());
  BigDecimal reduceAmount = originalPrice.subtract(ladder.getDiscount().multiply(originalPrice));
  cartPromotionItem.setReduceAmount(reduceAmount);
  cartPromotionItem.setRealStock(skuStock.getStock()-skuStock.getLockStock());
  BigDecimal reduceAmount = originalPrice.divide(totalAmount,RoundingMode.HALF_EVEN).multiply(fullReduction.getReducePrice());
  cartPromotionItem.setReduceAmount(reduceAmount);
  cartPromotionItem.setRealStock(skuStock.getStock()-skuStock.getLockStock());

代码示例来源: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: apache/hive

public static Timestamp decimalToTimestamp(HiveDecimalV1 dec) {
 try {
  BigDecimal nanoInstant = dec.bigDecimalValue().multiply(BILLION_BIG_DECIMAL);
  int nanos = nanoInstant.remainder(BILLION_BIG_DECIMAL).intValue();
  if (nanos < 0) {
   nanos += 1000000000;
  }
  long seconds =
    nanoInstant.subtract(new BigDecimal(nanos)).divide(BILLION_BIG_DECIMAL).longValue();
  Timestamp t = new Timestamp(seconds * 1000);
  t.setNanos(nanos);
  return t;
 } catch (NumberFormatException nfe) {
  return null;
 } catch (IllegalArgumentException iae) {
  return null;
 }
}

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

public BigDecimal calculateFeeBtc() {
 return roundUp(amount.multiply(new BigDecimal(.5))).divide(new BigDecimal(100.));
}

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

public BigDecimal decimalValue(MathContext mc){
  BigDecimal numerator = new BigDecimal(this.numerator);
  BigDecimal denominator = new BigDecimal(this.denominator);

  return numerator.divide(denominator, mc);
}

相关文章