java.math.MathContext.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(7.1k)|赞(0)|评价(0)|浏览(90)

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

MathContext.<init>介绍

[英]Constructs a new MathContext with the specified precision and with the rounding mode RoundingMode#HALF_UP. If the precision passed is zero, then this implies that the computations have to be performed exact, the rounding mode in this case is irrelevant.
[中]以指定的精度和舍入模式RoundingMode#HALF_UP构造新的MathContext。如果传递的精度为零,则这意味着必须精确执行计算,在这种情况下,舍入模式无关。

代码示例

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

public BigDecimal toBigDecimal()
{
  return new BigDecimal(unscaledValue, scale, new MathContext(precision));
}

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

// 1.
new BigDecimal("35.3456").round(new MathContext(4, RoundingMode.HALF_UP));
//result = 35.35
// 2.
new BigDecimal("35.3456").setScale(4, RoundingMode.HALF_UP);
// result = 35.3456

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

@Immutable
public static double round(double value, int precession, int mode){
  MathContext context;
  switch (mode){
    case 2: context = new MathContext(precession, RoundingMode.DOWN); break;
    case 3: context = new MathContext(precession, RoundingMode.HALF_EVEN); break;
    case 4:
      if ((long)value % 2 == 0)
        context = new MathContext(precession, RoundingMode.UP);
      else
        context = new MathContext(precession, RoundingMode.DOWN);
      break;
    default:
      context = new MathContext(precession, RoundingMode.UP);
  }
  return BigDecimal.valueOf(value)
      .round(context)
      .doubleValue();
}

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

public BigDecimal toBigDecimal()
{
  return new BigDecimal(unscaledValue, scale, new MathContext(precision));
}

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

@Override
  protected Number getNormalizedValue()
  {
    if (value.isNaN() || value.isInfinite()) {
      return value;
    }
    return new BigDecimal(getValue().floatValue()).round(new MathContext(precision)).floatValue();
  }
}

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

/**
 * Multiply and round.
 *
 * @param x The left factor.
 * @param y The right factor.
 * @return The product x*y.
 */
static public BigDecimal multiplyRound(final BigDecimal x, final BigDecimal y) {
  BigDecimal resul = x.multiply(y);
  /* The estimation of the relative error in the result is the sum of the relative
   * errors |err(y)/y|+|err(x)/x|
   */
  MathContext mc = new MathContext(Math.min(x.precision(), y.precision()));
  return resul.round(mc);
} /* multiplyRound */

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

/**
 * A {@link #toString()} variation that returns percent string rounded to the specified precision.
 *
 * @param precision precision for the returned String.
 * @return  percent string rounded to the specified precision.
 */
public String toString(int precision) {
  return new BigDecimal(percent, new MathContext(precision, RoundingMode.HALF_UP)) + "%";
}

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

@Override
  protected Number getNormalizedValue()
  {
    if (value.isNaN() || value.isInfinite()) {
      return value;
    }
    return new BigDecimal(getValue().doubleValue()).round(new MathContext(precision)).doubleValue();
  }
}

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

public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
 MathContext mc = new MathContext(scale);
 return getBigDecimal(columnIndex).round(mc);
}

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

System.out.println(new BigDecimal("123.4",
          new MathContext(4,RoundingMode.HALF_UP)));
System.out.println(new BigDecimal("123.4",
          new MathContext(2,RoundingMode.HALF_UP)));
System.out.println(new BigDecimal("123.4",
          new MathContext(2,RoundingMode.CEILING)));
System.out.println(new BigDecimal("123.4",
          new MathContext(1,RoundingMode.CEILING)));

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

/**
 * Rounded heap size in gigabytes.
 *
 * @param heap Heap.
 * @param precision Precision.
 * @return Rounded heap size.
 */
private static double roundedHeapSize(double heap, int precision) {
  double rounded = new BigDecimal(heap / (1024 * 1024 * 1024d)).round(new MathContext(precision)).doubleValue();
  return rounded < 0.1 ? 0.1 : rounded;
}

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

BadConstant(double base, double factor, String replacement, int basePriority) {
  this.base = base;
  this.factor = factor;
  this.value = this.base * this.factor;
  this.replacement = replacement;
  this.basePriority = basePriority;
  BigDecimal valueBig = BigDecimal.valueOf(value);
  BigDecimal baseBig = BigDecimal.valueOf(base);
  BigDecimal factorBig = BigDecimal.valueOf(factor);
  for (int prec = 0; prec < 14; prec++) {
    addApprox(baseBig.round(new MathContext(prec, RoundingMode.FLOOR)).multiply(factorBig));
    addApprox(baseBig.round(new MathContext(prec, RoundingMode.CEILING)).multiply(factorBig));
    addApprox(valueBig.round(new MathContext(prec, RoundingMode.FLOOR)));
    addApprox(valueBig.round(new MathContext(prec, RoundingMode.CEILING)));
  }
}

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

/**
 * Divide and round.
 *
 * @param x The numerator
 * @param n The denominator
 * @return the divided x/n
 */
static public BigDecimal divideRound(final BigDecimal x, final BigInteger n) {
  /* The estimation of the relative error in the result is |err(x)/x|
   */
  MathContext mc = new MathContext(x.precision());
  return x.divide(new BigDecimal(n), mc);
}

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

double d = ...;
BigDecimal bd = new BigDecimal(d);
bd = bd.round(new MathContext(3));
double rounded = bd.doubleValue();

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

/**
 * Subtract and round according to the larger of the two ulp’s.
 *
 * @param x The left term.
 * @param y The right term.
 * @return The difference x-y.
 */
static public BigDecimal subtractRound(final BigDecimal x, final BigDecimal y) {
  BigDecimal resul = x.subtract(y);
  /* The estimation of the absolute error in the result is |err(y)|+|err(x)|
   */
  double errR = Math.abs(y.ulp().doubleValue() / 2.) + Math.abs(x.ulp().doubleValue() / 2.);
  MathContext mc = new MathContext(err2prec(resul.doubleValue(), errR));
  return resul.round(mc);
} /* subtractRound */

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

/**
 * Divide and round.
 *
 * @param n The numerator
 * @param x The denominator
 * @return the divided n/x
 */
static public BigDecimal divideRound(final BigInteger n, final BigDecimal x) {
  /* The estimation of the relative error in the result is |err(x)/x|
   */
  MathContext mc = new MathContext(x.precision());
  return new BigDecimal(n).divide(x, mc);
}

代码示例来源:origin: h2oai/h2o-2

static double signifDigits(double x, int digits) {
  if(Double.isNaN(x)) return x;
  BigDecimal bd = new BigDecimal(x);
  bd = bd.round(new MathContext(digits, RoundingMode.HALF_EVEN));
  return bd.doubleValue();
 }
}

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

/**
 * Add and round according to the larger of the two ulp’s.
 *
 * @param x The left summand
 * @param y The right summand
 * @return The sum x+y.
 */
static public BigDecimal addRound(final BigDecimal x, final BigDecimal y) {
  BigDecimal resul = x.add(y);
  /* The estimation of the absolute error in the result is |err(y)|+|err(x)|
   */
  double errR = Math.abs(y.ulp().doubleValue() / 2.) + Math.abs(x.ulp().doubleValue() / 2.);
  MathContext mc = new MathContext(err2prec(resul.doubleValue(), errR));
  return resul.round(mc);
} /* addRound */

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

/**
 * Divide and round.
 *
 * @param n The numerator.
 * @param x The denominator.
 * @return the divided n/x.
 */
static public BigDecimal divideRound(final int n, final BigDecimal x) {
  /* The estimation of the relative error in the result is |err(x)/x|
   */
  MathContext mc = new MathContext(x.precision());
  return new BigDecimal(n).divide(x, mc);
}

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

/**
 * Multiply and round.
 *
 * @param x The left factor.
 * @param n The right factor.
 * @return The product x*n.
 */
static public BigDecimal multiplyRound(final BigDecimal x, final int n) {
  BigDecimal resul = x.multiply(new BigDecimal(n));
  /* The estimation of the absolute error in the result is |n*err(x)|
   */
  MathContext mc = new MathContext(n != 0 ? x.precision() : 0);
  return resul.round(mc);
}

相关文章