/** * Returns a {@code BigDecimal} array which contains the integral part of * {@code this / divisor} at index 0 and the remainder {@code this % * divisor} at index 1. The quotient is rounded down towards zero to the * next integer. * * @param divisor * value by which {@code this} is divided. * @return {@code [this.divideToIntegralValue(divisor), * this.remainder(divisor)]}. * @throws NullPointerException * if {@code divisor == null}. * @throws ArithmeticException * if {@code divisor == 0}. * @see #divideToIntegralValue * @see #remainder */ public BigDecimal[] divideAndRemainder(BigDecimal divisor) { BigDecimal quotAndRem[] = new BigDecimal[2]; quotAndRem[0] = this.divideToIntegralValue(divisor); quotAndRem[1] = this.subtract( quotAndRem[0].multiply(divisor) ); return quotAndRem; }
/** * Returns a {@code BigDecimal} array which contains the integral part of * {@code this / divisor} at index 0 and the remainder {@code this % * divisor} at index 1. The quotient is rounded down towards zero to the * next integer. The rounding mode passed with the parameter {@code mc} is * not considered. But if the precision of {@code mc > 0} and the integral * part requires more digits, then an {@code ArithmeticException} is thrown. * * @param divisor * value by which {@code this} is divided. * @param mc * math context which determines the maximal precision of the * result. * @return {@code [this.divideToIntegralValue(divisor), * this.remainder(divisor)]}. * @throws NullPointerException * if {@code divisor == null}. * @throws ArithmeticException * if {@code divisor == 0}. * @see #divideToIntegralValue * @see #remainder */ public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc) { BigDecimal quotAndRem[] = new BigDecimal[2]; quotAndRem[0] = this.divideToIntegralValue(divisor, mc); quotAndRem[1] = this.subtract( quotAndRem[0].multiply(divisor) ); return quotAndRem; }
return this.divideToIntegralValue(divisor);
/** * 获取该数被除后的整数 * * @param object * 因数 * * @return 结果 */ public Decimal getDivGetInteger(Object object) { return new Decimal(this.bigDecimal.divideToIntegralValue(new Decimal(object).getBigDecimal(), mathContext)); }
@Override public BigDecimal getQuantizedValue(BigDecimal attributeValue) { return attributeValue .divideToIntegralValue(compressionFactor) .multiply(compressionFactor) .setScale(0, RoundingMode.DOWN); } }
public BigInteger apply(BigDecimal... args) { if ( args.length != 2 ) return null; if ( args[1].equals( BigDecimal.ZERO ) ) return null; return args[0].divideToIntegralValue( args[1], MathContext.DECIMAL128 ).toBigIntegerExact(); }
@Override public BigInteger apply(final BigDecimal... args) { if (args.length != 2) return null; if (args[1].equals(BigDecimal.ZERO)) return null; return args[0].divideToIntegralValue(args[1], MathContext.DECIMAL128).toBigIntegerExact(); }
@Override public BigInteger apply(final BigDecimal... args) { if (args.length != 2) return null; if (args[1].equals(BigDecimal.ZERO)) return null; return args[0].divideToIntegralValue(args[1], MathContext.DECIMAL128).toBigIntegerExact(); }
public BigInteger apply(BigDecimal... args) { if ( args.length != 2 ) return null; if ( args[1].equals( BigDecimal.ZERO ) ) return null; return args[0].divideToIntegralValue( args[1], MathContext.DECIMAL128 ).toBigIntegerExact(); }
/** * Calculate the number of whole days (24 hour periods) for a given number of milliseconds. * * @param timestamp Time in milliseconds. * @return the number of whole days. */ public static final int daysInMilliSeconds(final long timestamp) { final BigDecimal days = new BigDecimal(timestamp); return days.divideToIntegralValue(new BigDecimal(1000 * 60 * 60 * 24)) .intValue(); }
private static float percent(BigDecimal value, BigDecimal total) { if (BigDecimal.ZERO.compareTo(total) < 0) { // 0 < total if (value.compareTo(BigDecimal.ZERO) <= 0) { // value <= 0 return 0f; } else { return value.multiply(PERCENT).divideToIntegralValue(total).floatValue(); } } else { return 0f; } } }
BigDecimal x = new BigDecimal("5521.0000000001"); x = x.add(new BigDecimal("-1"). multiply(x.divideToIntegralValue(new BigDecimal("1.0")))); System.out.println(x.toPlainString());
int digitsRemain = 4; BigDecimal bd = new BigDecimal("12.3456"); int power = bd.precision() - digitsRemain; BigDecimal unit = bd.ulp().scaleByPowerOfTen(power); BigDecimal result = bd.divideToIntegralValue(unit).multiply(unit);
@Override public RoundedMoney divideToIntegralValue(Number divisor) { BigDecimal dec = number.divideToIntegralValue(MoneyUtils.getBigDecimal(divisor), Optional.ofNullable( monetaryContext.get(MathContext.class)).orElse(MathContext.DECIMAL64)); return new RoundedMoney(dec, currency, rounding); }
public IntegerValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException { if (a instanceof IntegerValue && b instanceof IntegerValue) { return ((IntegerValue) a).idiv((IntegerValue) b); } final BigDecimal A = ((NumericValue) a).getDecimalValue(); final BigDecimal B = ((NumericValue) b).getDecimalValue(); if (B.signum() == 0) { throw new XPathException("Integer division by zero", "FOAR0001", c); } //BigInteger quot = A.divide(B, 0, BigDecimal.ROUND_DOWN).toBigInteger(); BigInteger quot = A.divideToIntegralValue(B).toBigInteger(); return BigIntegerValue.makeIntegerValue(quot); }
@Override public Money divideToIntegralValue(Number divisor) { if (NumberVerifier.isInfinityAndNotNaN(divisor)) { return Money.of(0, getCurrency()); } MathContext mc = MoneyUtils.getMathContext(monetaryContext, RoundingMode.HALF_EVEN); BigDecimal divisorBD = MoneyUtils.getBigDecimal(divisor); BigDecimal dec = this.number.divideToIntegralValue(divisorBD, mc); return new Money(dec, getCurrency(), monetaryContext); }
private RubyInteger idiv(ThreadContext context, RubyRational val) { if (isNaN()) throw newNaNFloatDomainError(context.runtime); if (isInfinity()) { // NOTE: MRI is inconsistent with div(other, d) impl throw newInfinityFloatDomainError(context.runtime, infinitySign); } if (val.isZero()) throw context.runtime.newZeroDivisionError(); BigDecimal result = this.value.multiply(toBigDecimal(val.getDenominator())) .divideToIntegralValue(toBigDecimal(val.getNumerator())); return toInteger(context.runtime, result); }
private RubyInteger idiv(ThreadContext context, RubyRational val) { if (isNaN()) throw newNaNFloatDomainError(context.runtime); if (isInfinity()) { // NOTE: MRI is inconsistent with div(other, d) impl throw newInfinityFloatDomainError(context.runtime, infinitySign); } if (val.isZero()) throw context.runtime.newZeroDivisionError(); BigDecimal result = this.value.multiply(toBigDecimal(val.getDenominator())) .divideToIntegralValue(toBigDecimal(val.getNumerator())); return toInteger(context.runtime, result); }
@Override public FastMoney divideToIntegralValue(Number divisor) { if (NumberVerifier.isInfinityAndNotNaN(divisor)) { return new FastMoney(0L, getCurrency()); } checkNumber(divisor); if (isOne(divisor)) { return this; } BigDecimal div = MoneyUtils.getBigDecimal(divisor); return new FastMoney(getBigDecimal().divideToIntegralValue(div), getCurrency(), false); }
/** 小数,整除 */ private static Number decimalAliquot(Number obj1, Number obj2) { int maxType = getNumericType(obj1, obj2); switch (maxType) { case FLOAT: return newReal(maxType, (int) (floatValue(obj1) / floatValue(obj2))); case DOUBLE: return newReal(maxType, (long) (doubleValue(obj1) / doubleValue(obj2))); default: return newReal(maxType, bigDecimalValue(obj1).divideToIntegralValue(bigDecimalValue(obj2))); } } /** 小数,求余 */