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

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

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

BigDecimal.precision介绍

[英]Represent the number of decimal digits in the unscaled value. This precision is calculated the first time, and used in the following calls of method precision(). Note that some call to the private method inplaceRound() could update this field.
[中]表示未标度值中的小数位数。此精度是第一次计算的,并在方法precision()的以下调用中使用。请注意,对私有方法inplaceRound()的某些调用可能会更新此字段。

代码示例

代码示例来源:origin: alibaba/canal

private static int getLength(BigDecimal v) {
  int signum = v.signum();
  if (signum == 0) { // Special case for zero
    return 1;
  }
  return (signum < 0 ? 2 : 1) + (v.precision() + 1 + (v.scale() % 2 == 0 ? 0 : 1)) / 2;
}

代码示例来源:origin: com.fasterxml.jackson.datatype/jackson-datatype-jsr310

if (nanoseconds.precision() - nanoseconds.scale() <= 0) {
else if (seconds.scale() < -63) {

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

BigDecimal noZero = b.stripTrailingZeros();
int scale = noZero.scale();
int precision = noZero.precision();    
if (scale < 0) { // Adjust for negative scale
  precision -= scale;
  scale = 0;        
}

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

public static SqlDecimal of(String decimalValue)
{
  BigDecimal bigDecimal = new BigDecimal(decimalValue);
  return new SqlDecimal(bigDecimal.unscaledValue(), bigDecimal.precision(), bigDecimal.scale());
}

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

public static SqlDecimal of(String decimalValue)
{
  BigDecimal bigDecimal = new BigDecimal(decimalValue);
  return new SqlDecimal(bigDecimal.unscaledValue(), bigDecimal.precision(), bigDecimal.scale());
}

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

public Decimal18Expression(BigDecimal input, ExpressionPosition pos) {
 super(pos);
 this.scale = input.scale();
 this.precision = input.precision();
 this.decimal = input.setScale(scale, BigDecimal.ROUND_HALF_UP).longValue();
}

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

public Decimal9Expression(BigDecimal input, ExpressionPosition pos) {
 super(pos);
 this.scale = input.scale();
 this.precision = input.precision();
 this.decimal = input.setScale(scale, BigDecimal.ROUND_HALF_UP).intValue();
}

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

private BigDecimal parseBigDecimal(Slice slice, int offset, int length)
  {
    checkArgument(length < buffer.length);
    for (int i = 0; i < length; i++) {
      buffer[i] = (char) slice.getByte(offset + i);
    }

    BigDecimal decimal = new BigDecimal(buffer, 0, length);

    checkState(decimal.scale() <= type.getScale(), "Read decimal value scale larger than column scale");
    decimal = decimal.setScale(type.getScale(), HALF_UP);
    checkState(decimal.precision() <= type.getPrecision(), "Read decimal precision larger than column precision");
    return decimal;
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

public Number divideImpl(Number left, Number right) {
  BigDecimal bigLeft = toBigDecimal(left);
  BigDecimal bigRight = toBigDecimal(right);
  try {
    return bigLeft.divide(bigRight);
  } catch (ArithmeticException e) {
    // set a DEFAULT precision if otherwise non-terminating
    int precision = Math.max(bigLeft.precision(), bigRight.precision()) + DIVISION_EXTRA_PRECISION;
    BigDecimal result = bigLeft.divide(bigRight, new MathContext(precision));
    int scale = Math.max(Math.max(bigLeft.scale(), bigRight.scale()), DIVISION_MIN_SCALE);
    if (result.scale() > scale) result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
    return result;
  }
}

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

/**
 * Returns the number of digits (integer and fractional) in the number, which is equivalent
 * to SQL decimal precision. Note that this is different from BigDecimal.precision(),
 * which returns the precision of the unscaled value (BigDecimal.valueOf(0.01).precision() = 1,
 * whereas HiveDecimal.create("0.01").precision() = 2).
 * If you want the BigDecimal precision, use HiveDecimal.bigDecimalValue().precision()
 * @return
 */
@HiveDecimalVersionV1
public int precision() {
 int bdPrecision = bd.precision();
 int bdScale = bd.scale();
 if (bdPrecision < bdScale) {
  // This can happen for numbers less than 0.1
  // For 0.001234: bdPrecision=4, bdScale=6
  // In this case, we'll set the type to have the same precision as the scale.
  return bdScale;
 }
 return bdPrecision;
}

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

final long mag = ((long) record.scale()) - ((long) record.precision()) + 1;

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

private static BigDecimal enforcePrecisionScale(BigDecimal bd, int maxPrecision, int maxScale) {
 if (bd == null) {
  return null;
 }
 /**
  * Specially handling the case that bd=0, and we are converting it to a type where precision=scale,
  * such as decimal(1, 1).
  */
 if (bd.compareTo(BigDecimal.ZERO) == 0 && bd.scale() == 0 && maxPrecision == maxScale) {
  return bd.setScale(maxScale);
 }
 bd = trim(bd);
 if (bd.scale() > maxScale) {
  bd = bd.setScale(maxScale, RoundingMode.HALF_UP);
 }
 int maxIntDigits = maxPrecision - maxScale;
 int intDigits = bd.precision() - bd.scale();
 if (intDigits > maxIntDigits) {
  return null;
 }
 return bd;
}

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

private static BigDecimal normalize(BigDecimal bd, boolean allowRounding) {
 if (bd == null) {
  return null;
 }
 bd = trim(bd);
 int intDigits = bd.precision() - bd.scale();
 if (intDigits > MAX_PRECISION) {
  return null;
 }
 int maxScale = Math.min(MAX_SCALE, Math.min(MAX_PRECISION - intDigits, bd.scale()));
 if (bd.scale() > maxScale ) {
  if (allowRounding) {
   bd = bd.setScale(maxScale, RoundingMode.HALF_UP);
   // Trimming is again necessary, because rounding may introduce new trailing 0's.
   bd = trim(bd);
  } else {
   bd = null;
  }
 }
 return bd;
}

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

public static VarDecimalHolder getVarDecimalHolder(DrillBuf buf, BigDecimal bigDecimal) {
  VarDecimalHolder dch = new VarDecimalHolder();

  byte[] bytes = bigDecimal.unscaledValue().toByteArray();
  int length = bytes.length;

  dch.scale = bigDecimal.scale();
  dch.precision = bigDecimal.precision();
  dch.start = 0;
  dch.end = length;
  dch.buffer = buf.reallocIfNeeded(length);
  dch.buffer.setBytes(0, bytes);

  return dch;
 }
}

代码示例来源:origin: org.mongodb/mongo-java-driver

private BigDecimal clampAndRound(final BigDecimal initialValue) {
  BigDecimal value;
  if (-initialValue.scale() > MAX_EXPONENT) {
    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);
  } else if (-initialValue.scale() < MIN_EXPONENT) {
  } else {
    value = initialValue.round(DECIMAL128);
    int extraPrecision = initialValue.precision() - value.precision();
    if (extraPrecision > 0) {

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

@Override
public SqlDecimal getExpectedValue(int start, int length)
{
  if (length == 0) {
    return null;
  }
  BigDecimal avg = BigDecimal.ZERO;
  for (int i = start; i < start + length; i++) {
    avg = avg.add(getBigDecimalForCounter(i));
  }
  avg = avg.divide(BigDecimal.valueOf(length), ROUND_HALF_UP);
  return new SqlDecimal(avg.unscaledValue(), avg.precision(), avg.scale());
}

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

if (bigDecimal.scale() != 0) {
 if (bigDecimal.signum() != 0) {
  BigDecimal bigDecimalStripped = bigDecimal.stripTrailingZeros();
  int stripTrailingZerosScale = bigDecimalStripped.scale();
 int scale = bigDecimal.scale();
 if (scale < 0 || scale > HiveDecimal.MAX_SCALE) {
  return false;
if (!fastSetFromBigInteger(bigDecimal.unscaledValue(), bigDecimal.scale(),
  bigDecimal.precision(), fastResult)) {
 return false;

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

@Override
public SqlDecimal getExpectedValue(int start, int length)
{
  if (length == 0) {
    return null;
  }
  BigDecimal sum = BigDecimal.ZERO;
  for (int i = start; i < start + length; i++) {
    sum = sum.add(getBigDecimalForCounter(i));
  }
  return new SqlDecimal(sum.unscaledValue(), sum.precision(), sum.scale());
}

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

public static Decimal38SparseHolder getDecimal38Holder(DrillBuf buf, BigDecimal bigDecimal) {
 Decimal38SparseHolder dch = new Decimal38SparseHolder();
 dch.scale = bigDecimal.scale();
 dch.precision = bigDecimal.precision();
 Decimal38SparseHolder.setSign(bigDecimal.signum() == -1, dch.start, dch.buffer);
 dch.start = 0;
 dch.buffer = buf.reallocIfNeeded(Decimal38SparseHolder.maxPrecision * DecimalUtility.INTEGER_SIZE);
 DecimalUtility
  .getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, Decimal38SparseHolder.nDecimalDigits);
 return dch;
}

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

public static Decimal28SparseHolder getDecimal28Holder(DrillBuf buf, BigDecimal bigDecimal) {
 Decimal28SparseHolder dch = new Decimal28SparseHolder();
 dch.scale = bigDecimal.scale();
 dch.precision = bigDecimal.precision();
 Decimal28SparseHolder.setSign(bigDecimal.signum() == -1, dch.start, dch.buffer);
 dch.start = 0;
 dch.buffer = buf.reallocIfNeeded(5 * DecimalUtility.INTEGER_SIZE);
 DecimalUtility
  .getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, Decimal28SparseHolder.nDecimalDigits);
 return dch;
}

相关文章