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

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

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

BigDecimal.scale介绍

[英]The 32-bit integer scale in the internal representation of BigDecimal.
[中]BigDecimal内部表示形式中的32位整数刻度。

代码示例

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

private static BigDecimal normalizeDecimal(BigDecimal input) {
  if (input.scale() < 0) {
    return input.setScale(0);
  } else {
    return input;
  }
}

代码示例来源:origin: kiegroup/optaplanner

/**
 * All parameters must have the same {@link BigDecimal#scale()}.
 * @param from never null, inclusive minimum
 * @param to never null, exclusive maximum, {@code >= from}
 */
public BigDecimalValueRange(BigDecimal from, BigDecimal to) {
  this(from, to, BigDecimal.valueOf(1L, from.scale()));
}

代码示例来源:origin: knowm/XChange

private int numberOfDecimals(String value) {
 return new BigDecimal(value).stripTrailingZeros().scale();
}

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

public static Decimal createThriftDecimal(String s) {
 BigDecimal d = new BigDecimal(s);
 return new Decimal((short) d.scale(), ByteBuffer.wrap(d.unscaledValue().toByteArray()));
}

代码示例来源:origin: kiegroup/optaplanner

public static long parseMicroCost(String costString) {
  BigDecimal costBigDecimal = new BigDecimal(costString);
  if (costBigDecimal.scale() > 11) {
    throw new IllegalArgumentException("The costString (" + costString + ") has a scale ("
        + costBigDecimal.scale() + ") higher than 10.");
  }
  costBigDecimal = costBigDecimal.setScale(11);
  return costBigDecimal.multiply(MICROS_PER_ONE_AS_BIG_DECIMAL).longValueExact();
}

代码示例来源:origin: kiegroup/optaplanner

@Override
public HardMediumSoftBigDecimalScore multiply(double multiplicand) {
  // Intentionally not taken "new BigDecimal(multiplicand, MathContext.UNLIMITED)"
  // because together with the floor rounding it gives unwanted behaviour
  BigDecimal multiplicandBigDecimal = BigDecimal.valueOf(multiplicand);
  // The (unspecified) scale/precision of the multiplicand should have no impact on the returned scale/precision
  return new HardMediumSoftBigDecimalScore(
      (int) Math.floor(initScore * multiplicand),
      hardScore.multiply(multiplicandBigDecimal).setScale(hardScore.scale(), RoundingMode.FLOOR),
      mediumScore.multiply(multiplicandBigDecimal).setScale(mediumScore.scale(), RoundingMode.FLOOR),
      softScore.multiply(multiplicandBigDecimal).setScale(softScore.scale(), RoundingMode.FLOOR));
}

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

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: json-path/JsonPath

private static Number unwrapNumber(final Number n) {
  Number unwrapped;
  if (!isPrimitiveNumber(n)) {
    BigDecimal bigDecimal = new BigDecimal(n.toString());
    if (bigDecimal.scale() <= 0) {
      if (bigDecimal.compareTo(new BigDecimal(Integer.MAX_VALUE)) <= 0) {
        unwrapped = bigDecimal.intValue();
      } else if (bigDecimal.compareTo(new BigDecimal(Long.MAX_VALUE)) <= 0){
        unwrapped = bigDecimal.longValue();
      } else {
        unwrapped = bigDecimal;
      }
    } else {
      final double doubleValue = bigDecimal.doubleValue();
      if (BigDecimal.valueOf(doubleValue).compareTo(bigDecimal) != 0) {
        unwrapped = bigDecimal;
      } else {
        unwrapped = doubleValue;
      }
    }
  } else {
    unwrapped = n;
  }
  return unwrapped;
}

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

if (nanoseconds.precision() - nanoseconds.scale() <= 0) {
else if (seconds.scale() < -63) {
  nanosOnly = nanoseconds.subtract(new BigDecimal(secondsOnly).scaleByPowerOfTen(9)).intValue();

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

BigInteger unscaled = bd.unscaledValue();
 int precision = bd.unscaledValue().abs().toString().length();
 int scale = bd.scale();
 RelDataType relType;
case INTERVAL_YEAR_MONTH:
 BigDecimal totalMonths = BigDecimal.valueOf(((HiveIntervalYearMonth) value).getTotalMonths());
 calciteLiteral = rexBuilder.makeIntervalLiteral(totalMonths,
   new SqlIntervalQualifier(TimeUnit.YEAR, TimeUnit.MONTH, new SqlParserPos(1,1)));
  .valueOf(((HiveIntervalDayTime) value).getTotalSeconds() * 1000);
  BigDecimal nanosValueBd = BigDecimal.valueOf(((HiveIntervalDayTime)
  value).getNanos(), 6);
  calciteLiteral =

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

public static Decimal toMetastoreDecimal(BigDecimal decimal)
{
  return new Decimal(ByteBuffer.wrap(decimal.unscaledValue().toByteArray()), (short) decimal.scale());
}

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

@Override
public void serialize(BigDecimal value, ByteBuffer out) {
  if (value == null) {
    BytesUtil.writeVInt(0, out);
    BytesUtil.writeVInt(-1, out);
    return;
  }
  if (value.scale() > type.getScale()) {
    if (avoidVerbose++ % 10000 == 0) {
      logger.warn("value's scale has exceeded the " + type.getScale() + ", cut it off, to ensure encoded value do not exceed maxLength " + maxLength + " times:" + (avoidVerbose));
    }
    value = value.setScale(type.getScale(), BigDecimal.ROUND_HALF_EVEN);
  }
  byte[] bytes = value.unscaledValue().toByteArray();
  if (bytes.length + 2 > maxLength) {
    throw new IllegalArgumentException("'" + value + "' exceeds the expected length for type " + type);
  }
  BytesUtil.writeVInt(value.scale(), out);
  BytesUtil.writeVInt(bytes.length, out);
  out.put(bytes);
}

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

static Number computeValue(final String value) {
  final BigDecimal bigDecimalValue = new BigDecimal(value);
  if (bigDecimalValue.scale() > 0) {
    return bigDecimalValue;
  }
  return bigDecimalValue.toBigInteger();
}

代码示例来源:origin: lealone/Lealone

@Override
public Value divide(Value v) {
  ValueDecimal dec = (ValueDecimal) v;
  if (dec.value.signum() == 0) {
    throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
  }
  BigDecimal bd = value.divide(dec.value, value.scale() + DIVIDE_SCALE_ADD, BigDecimal.ROUND_HALF_DOWN);
  if (bd.signum() == 0) {
    bd = BigDecimal.ZERO;
  } else if (bd.scale() > 0) {
    if (!bd.unscaledValue().testBit(0)) {
      String s = bd.toString();
      int i = s.length() - 1;
      while (i >= 0 && s.charAt(i) == '0') {
        i--;
      }
      if (i < s.length() - 1) {
        s = s.substring(0, i + 1);
        bd = new BigDecimal(s);
      }
    }
  }
  return ValueDecimal.get(bd);
}

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

public static BigDecimal parseHiveDecimal(byte[] bytes, int start, int length, DecimalType columnType)
  {
    BigDecimal parsed = new BigDecimal(new String(bytes, start, length, UTF_8));
    if (parsed.scale() > columnType.getScale()) {
      // Hive rounds HALF_UP too
      parsed = parsed.setScale(columnType.getScale(), HALF_UP);
    }
    return rescale(parsed, columnType);
  }
}

相关文章