/** * 人民币金额单位转换,分转换成元,取两位小数 例如:150 => 1.5 */ public static BigDecimal fen2yuan(BigDecimal num) { return num.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP); }
public BigDecimal getAveragePrice() { return low.add(high).divide(new BigDecimal("2")); }
@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; } }
public BigDecimal calculateFeeBtc() { return roundUp(amount.multiply(new BigDecimal(.5))).divide(new BigDecimal(100.)); }
@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(); }
@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); }
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); }
@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); }
/** * <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); } }
/** * 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(); }
/** * 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); } }
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; } }
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());
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; }
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; } }
public BigDecimal calculateFeeBtc() { return roundUp(amount.multiply(new BigDecimal(.5))).divide(new BigDecimal(100.)); }
public BigDecimal decimalValue(MathContext mc){ BigDecimal numerator = new BigDecimal(this.numerator); BigDecimal denominator = new BigDecimal(this.denominator); return numerator.divide(denominator, mc); }