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

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

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

BigDecimal.divideAndRemainder介绍

[英]Returns a BigDecimal array which contains the integral part of this / divisor at index 0 and the remainder this % at index 1. The quotient is rounded down towards zero to the next integer.
[中]返回一个BigDecimal数组,该数组在索引0处包含此/除数的整数部分,在索引1处包含此%的余数。商向下四舍五入到下一个整数。

代码示例

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

/**
 * Returns a new {@code BigDecimal} whose value is {@code this % divisor}.
 * <p>
 * The remainder is defined as {@code this -
 * this.divideToIntegralValue(divisor) * divisor}.
 *
 * @param divisor
 *            value by which {@code this} is divided.
 * @return {@code this % divisor}.
 * @throws NullPointerException
 *             if {@code divisor == null}.
 * @throws ArithmeticException
 *             if {@code divisor == 0}.
 */
public BigDecimal remainder(BigDecimal divisor) {
  return divideAndRemainder(divisor)[1];
}

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

/**
 * Returns a new {@code BigDecimal} whose value is {@code this % divisor}.
 * <p>
 * The remainder is defined as {@code this -
 * this.divideToIntegralValue(divisor) * divisor}.
 * <p>
 * The specified rounding mode {@code mc} is used for the division only.
 *
 * @param divisor
 *            value by which {@code this} is divided.
 * @param mc
 *            rounding mode and precision to be used.
 * @return {@code this % divisor}.
 * @throws NullPointerException
 *             if {@code divisor == null}.
 * @throws ArithmeticException
 *             if {@code divisor == 0}.
 * @throws ArithmeticException
 *             if {@code mc.getPrecision() > 0} and the result of {@code
 *             this.divideToIntegralValue(divisor, mc)} requires more digits
 *             to be represented.
 */
public BigDecimal remainder(BigDecimal divisor, MathContext mc) {
  return divideAndRemainder(divisor, mc)[1];
}

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

private Number mod(int maxType, Number left, Number right) {
    switch (maxType) {
    case INT:
      return Integer.valueOf(left.intValue() % right.intValue());
    case LONG:
      return Long.valueOf(left.longValue() % right.longValue());
    case FLOAT:
      return Float.valueOf(left.floatValue() % right.floatValue());
    case DOUBLE:
      return Double.valueOf(left.doubleValue() % right.doubleValue());
    case BIGDECIMAL:
      BigDecimal[] bd = toBigDecimals(left, right);
      return (bd[0]).divideAndRemainder(bd[1])[1];
    }
    throw new TemplateException("Unsupported data type", location);
  }
}

代码示例来源:origin: KylinOLAP/Kylin

public static BigDecimal mod(BigDecimal b0, BigDecimal b1) {
  final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
  return bigDecimals[1];
}

代码示例来源:origin: youseries/ureport

private Object calculate(Object left,char op,Object right){
  if((op=='*' || op=='/' || op=='%' || op=='-')){
    if(right instanceof String || left instanceof String){
      return left.toString()+op+right.toString();
    }
    BigDecimal b1=(BigDecimal)left;
    BigDecimal b2=(BigDecimal)right;
    if(op=='*'){
      return b1.multiply(b2);
    }else if(op=='/'){
      return b1.divide(b2,10,RoundingMode.HALF_UP).stripTrailingZeros();
    }else if(op=='%'){
      return b1.divideAndRemainder(b2)[1];
    }else if(op=='-'){
      return b1.subtract(b2);
    }
  }else if(op=='+'){
    if(right instanceof String || left instanceof String){
      return left.toString()+right.toString();
    }else{
      BigDecimal b1=(BigDecimal)left;
      BigDecimal b2=(BigDecimal)right;
      return b1.add(b2);
    }
  }
  throw new RuntimeException("Unkown operate "+op+"");
}

代码示例来源:origin: pholser/junit-quickcheck

@Property public void roundingUp(@Precision(scale = 8) BigDecimal d) {
    BigDecimal[] pieces = d.divideAndRemainder(ONE);
    BigDecimal integral = pieces[0];
    BigDecimal fractional = pieces[1];

    assumeThat(fractional, greaterThan(new BigDecimal("0.5")));

    DollarsAndCents money = new DollarsAndCents(d);

    assertEquals(
      integral.add(fractional).setScale(2, HALF_UP),
      money.toBigDecimal());
  }
}

代码示例来源:origin: pholser/junit-quickcheck

@Property public void roundingDown(@Precision(scale = 8) BigDecimal d) {
  BigDecimal[] pieces = d.divideAndRemainder(ONE);
  BigDecimal integral = pieces[0];
  BigDecimal fractional = pieces[1];
  assumeThat(fractional, lessThanOrEqualTo(new BigDecimal("0.5")));
  DollarsAndCents money = new DollarsAndCents(d);
  assertEquals(
    integral.add(fractional).setScale(2, HALF_DOWN),
    money.toBigDecimal());
}

代码示例来源:origin: Qihoo360/Quicksql

public static BigDecimal mod(BigDecimal b0, BigDecimal b1) {
 final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
 return bigDecimals[1];
}

代码示例来源:origin: Qihoo360/Quicksql

public static BigDecimal floor(BigDecimal b0, BigDecimal b1) {
 final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
 BigDecimal r = bigDecimals[1];
 if (r.signum() < 0) {
  r = r.add(b1);
 }
 return b0.subtract(r);
}

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

public static BigDecimal floor(BigDecimal b0, BigDecimal b1) {
 final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
 BigDecimal r = bigDecimals[1];
 if (r.signum() < 0) {
  r = r.add(b1);
 }
 return b0.subtract(r);
}

代码示例来源:origin: Qihoo360/Quicksql

public static BigDecimal ceil(BigDecimal b0, BigDecimal b1) {
 final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
 BigDecimal r = bigDecimals[1];
 if (r.signum() > 0) {
  r = r.subtract(b1);
 }
 return b0.subtract(r);
}

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

public static BigDecimal ceil(BigDecimal b0, BigDecimal b1) {
 final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
 BigDecimal r = bigDecimals[1];
 if (r.signum() > 0) {
  r = r.subtract(b1);
 }
 return b0.subtract(r);
}

代码示例来源:origin: org.apache.kylin/atopcalcite

public static BigDecimal floor(BigDecimal b0, BigDecimal b1) {
  final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
  BigDecimal r = bigDecimals[1];
  if (r.signum() < 0) {
    r = r.add(b1);
  }
  return b0.subtract(r);
}

代码示例来源:origin: org.apache.kylin/atopcalcite

public static BigDecimal ceil(BigDecimal b0, BigDecimal b1) {
  final BigDecimal[] bigDecimals = b0.divideAndRemainder(b1);
  BigDecimal r = bigDecimals[1];
  if (r.signum() > 0) {
    r = r.subtract(b1);
  }
  return b0.subtract(r);
}

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

long minutes = 230L;
BigDecimal min = new BigDecimal(minutes);
BigDecimal constant = new BigDecimal(60);
BigDecimal[] val=min.divideAndRemainder(constant);
System.out.println(val[0]+"."+val[1]+" Hours");

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

BigDecimal numberOne = new BigDecimal("10");
BigDecimal numberTwo = new BigDecimal("35.55");
BigDecimal[] divRem = numberTwo.divideAndRemainder(numberOne);
System.out.println("The second number fits " + divRem[0].stripTrailingZeros().toPlainString() + 
          " times into the first one. The rest is: " + divRem[1].stripTrailingZeros().toPlainString());

代码示例来源:origin: org.appdapter/ext.bundle.math.symja_jas

/**
 * BigDecimal compute quotient and remainder.
 * @param S BigDecimal.
 * @return BigDecimal[] { q, r } with q = this/S and r = rem(this,S).
 */
public BigDecimal[] quotientRemainder(BigDecimal S) {
  BigDecimal[] qr = new BigDecimal[2];
  java.math.BigDecimal[] C = val.divideAndRemainder(S.val, context);
  qr[0] = new BigDecimal(C[0]);
  qr[1] = new BigDecimal(C[1]);
  return qr;
}

代码示例来源:origin: UniversaBlockchain/universa

public Decimal[] divideAndRemainder(BigDecimal divisor) {
  BigDecimal[] result = value.divideAndRemainder(divisor);
  return new Decimal[]{new Decimal(result[0]), new Decimal(result[1])};
}

代码示例来源:origin: timolson/cointrader

@Transient
public DecimalAmount dividedBy(BigDecimal o, RemainderHandler remainderHandler) {
 BigDecimal[] divideAndRemainder = asBigDecimal().divideAndRemainder(o, remainderHandler.getMathContext());
 DecimalAmount result = new DecimalAmount(divideAndRemainder[0]);
 remainderHandler.handleRemainder(result, divideAndRemainder[1]);
 return result;
}

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

@Override
public RoundedMoney[] divideAndRemainder(Number divisor) {
  BigDecimal bd = MoneyUtils.getBigDecimal(divisor);
  if (isOne(bd)) {
    return new RoundedMoney[]{this, new RoundedMoney(0L, getCurrency(), rounding)};
  }
  BigDecimal[] dec = number.divideAndRemainder(MoneyUtils.getBigDecimal(divisor), Optional.ofNullable(
      monetaryContext.get(MathContext.class)).orElse(MathContext.DECIMAL64));
  return new RoundedMoney[]{new RoundedMoney(dec[0], currency, rounding),
      new RoundedMoney(dec[1], currency, rounding).with(rounding)};
}

相关文章