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

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

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

BigDecimal.round介绍

[英]Returns a new BigDecimal whose value is this, rounded according to the passed context mc.

If mc.precision = 0, then no rounding is performed.

If mc.precision > 0 and mc.roundingMode == UNNECESSARY, then an ArithmeticException is thrown if the result cannot be represented exactly within the given 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: 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: org.apache.poi/poi

public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) {
  final double value;
  if (number instanceof Number) {
    value = ((Number)number).doubleValue();
    if (Double.isInfinite(value) || Double.isNaN(value)) {
      return integerFormat.format(number, toAppendTo, pos);
    }
  } else {
    // testBug54786 gets here with a date, so retain previous behaviour
    return integerFormat.format(number, toAppendTo, pos);
  }
  final double abs = Math.abs(value);
  if (abs >= 1E11 || (abs <= 1E-10 && abs > 0)) {
    return scientificFormat.format(number, toAppendTo, pos);
  } else if (Math.floor(value) == value || abs >= 1E10) {
    // integer, or integer portion uses all 11 allowed digits
    return integerFormat.format(number, toAppendTo, pos);
  }
  // Non-integers of non-scientific magnitude are formatted as "up to 11
  // numeric characters, with the decimal point counting as a numeric
  // character". We know there is a decimal point, so limit to 10 digits.
  // https://support.microsoft.com/en-us/kb/65903
  final double rounded = new BigDecimal(value).round(TO_10_SF).doubleValue();
  return decimalFormat.format(rounded, toAppendTo, pos);
}

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

public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) {
  final double value;
  if (number instanceof Number) {
    value = ((Number)number).doubleValue();
    if (Double.isInfinite(value) || Double.isNaN(value)) {
      return integerFormat.format(number, toAppendTo, pos);
    }
  } else {
    // testBug54786 gets here with a date, so retain previous behaviour
    return integerFormat.format(number, toAppendTo, pos);
  }
  final double abs = Math.abs(value);
  if (abs >= 1E11 || (abs <= 1E-10 && abs > 0)) {
    return scientificFormat.format(number, toAppendTo, pos);
  } else if (Math.floor(value) == value || abs >= 1E10) {
    // integer, or integer portion uses all 11 allowed digits
    return integerFormat.format(number, toAppendTo, pos);
  }
  // Non-integers of non-scientific magnitude are formatted as "up to 11
  // numeric characters, with the decimal point counting as a numeric
  // character". We know there is a decimal point, so limit to 10 digits.
  // https://support.microsoft.com/en-us/kb/65903
  final double rounded = new BigDecimal(value).round(TO_10_SF).doubleValue();
  return decimalFormat.format(rounded, toAppendTo, pos);
}

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

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

代码示例来源: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: org.mongodb/mongo-java-driver

int diff = -initialValue.scale() - MAX_EXPONENT;
  if (initialValue.unscaledValue().equals(BIG_INT_ZERO)) {
    value = new BigDecimal(initialValue.unscaledValue(), -MAX_EXPONENT);
  } else if (diff + initialValue.precision() > 34) {
    throw new NumberFormatException("Exponent is out of range for Decimal128 encoding of " + initialValue);
  } else {
    BigInteger multiplier = BIG_INT_TEN.pow(diff);
    value = new BigDecimal(initialValue.unscaledValue().multiply(multiplier), initialValue.scale() + diff);
  int undiscardedPrecision = ensureExactRounding(initialValue, diff);
  BigInteger divisor = undiscardedPrecision == 0 ? BIG_INT_ONE : BIG_INT_TEN.pow(diff);
  value = new BigDecimal(initialValue.unscaledValue().divide(divisor), initialValue.scale() - diff);
} else {
  value = initialValue.round(DECIMAL128);
  int extraPrecision = initialValue.precision() - value.precision();
  if (extraPrecision > 0) {

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

return subtract(subtrahend).round(mc);
    leftOperand = new BigDecimal(tempBI, this.scale + 1);
    return leftOperand.round(mc);
return subtract(subtrahend).round(mc);

代码示例来源: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: stackoverflow.com

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

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

return add(augend).round(mc);
  smaller = augend;
} else {// No optimization is done
  return add(augend).round(mc);
  return add(augend).round(mc);
larger = new BigDecimal(tempBI, larger.scale + 1);
return larger.round(mc);

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

public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) {
  final double value;
  if (number instanceof Number) {
    value = ((Number) number).doubleValue();
    if (Double.isInfinite(value) || Double.isNaN(value)) {
      return integerFormat.format(number, toAppendTo, pos);
    }
  } else {
    // testBug54786 gets here with a date, so retain previous behaviour
    return integerFormat.format(number, toAppendTo, pos);
  }
  final double abs = Math.abs(value);
  if (abs > 1E15 || (abs <= 1E-15 && abs > 0)) {
    return scientificFormat.format(number, toAppendTo, pos);
  } else if (Math.floor(value) == value || abs > 1E15) {
    // integer, or integer portion uses all 15 allowed digits
    return integerFormat.format(number, toAppendTo, pos);
  }
  // Non-integers of non-scientific magnitude are formatted as "up to 11
  // numeric characters, with the decimal point counting as a numeric
  // character". We know there is a decimal point, so limit to 10 digits.
  // https://support.microsoft.com/en-us/kb/65903
  final double rounded = new BigDecimal(value).round(TO_15_SF).doubleValue();
  return decimalFormat.format(rounded, toAppendTo, pos);
}

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

/**
 * 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);
}

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

import java.math.BigDecimal;
import java.math.MathContext;

public class Test {
  public static void main(String[] args) {
    //double d = ((0.053800 * (500000/1000)) + 4) * 0.85;
    BigDecimal d = ((new BigDecimal(0.053800).multiply(new BigDecimal(500000).divide(new BigDecimal(1000)))).add(new BigDecimal(4))).multiply(new BigDecimal(0.85));
    System.out.println(d.round(MathContext.DECIMAL32));
  }
}

相关文章