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

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

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

BigDecimal.toBigIntegerExact介绍

[英]Returns this BigDecimal as a big integer instance if it has no fractional part. If this BigDecimal has a fractional part, i.e. if rounding would be necessary, an ArithmeticException is thrown.
[中]如果没有小数部分,则将此BigDecimal作为大整数实例返回。如果此BigDecimal具有小数部分,即如果需要舍入,则会引发算术异常。

代码示例

代码示例来源:origin: google/guava

/**
 * Returns the result of dividing {@code p} by {@code q}, rounding using the specified {@code
 * RoundingMode}.
 *
 * @throws ArithmeticException if {@code q == 0}, or if {@code mode == UNNECESSARY} and {@code a}
 *     is not an integer multiple of {@code b}
 */
@GwtIncompatible // TODO
public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode) {
 BigDecimal pDec = new BigDecimal(p);
 BigDecimal qDec = new BigDecimal(q);
 return pDec.divide(qDec, 0, mode).toBigIntegerExact();
}

代码示例来源:origin: google/error-prone

private static Optional<BigInteger> asBigInteger(BigDecimal v) {
 try {
  return Optional.of(v.toBigIntegerExact());
 } catch (ArithmeticException e) {
  return Optional.empty();
 }
}

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

/**
 * Returns the result of dividing {@code p} by {@code q}, rounding using the specified {@code
 * RoundingMode}.
 *
 * @throws ArithmeticException if {@code q == 0}, or if {@code mode == UNNECESSARY} and {@code a}
 *     is not an integer multiple of {@code b}
 */
@GwtIncompatible // TODO
public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode) {
 BigDecimal pDec = new BigDecimal(p);
 BigDecimal qDec = new BigDecimal(q);
 return pDec.divide(qDec, 0, mode).toBigIntegerExact();
}

代码示例来源:origin: graphql-java/graphql-java

private BigInteger convertImpl(Object input) {
  if (isNumberIsh(input)) {
    BigDecimal value;
    try {
      value = new BigDecimal(input.toString());
    } catch (NumberFormatException e) {
      return null;
    }
    try {
      return value.toBigIntegerExact();
    } catch (ArithmeticException e) {
      return null;
    }
  }
  return null;
}

代码示例来源:origin: google/j2objc

/**
 * Returns the result of dividing {@code p} by {@code q}, rounding using the specified {@code
 * RoundingMode}.
 *
 * @throws ArithmeticException if {@code q == 0}, or if {@code mode == UNNECESSARY} and {@code a}
 *     is not an integer multiple of {@code b}
 */
@GwtIncompatible // TODO
public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode) {
 BigDecimal pDec = new BigDecimal(p);
 BigDecimal qDec = new BigDecimal(q);
 return pDec.divide(qDec, 0, mode).toBigIntegerExact();
}

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

/**
 * Returns the result of dividing {@code p} by {@code q}, rounding using the specified {@code
 * RoundingMode}.
 *
 * @throws ArithmeticException if {@code q == 0}, or if {@code mode == UNNECESSARY} and {@code a}
 *     is not an integer multiple of {@code b}
 */
@GwtIncompatible // TODO
public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode) {
 BigDecimal pDec = new BigDecimal(p);
 BigDecimal qDec = new BigDecimal(q);
 return pDec.divide(qDec, 0, mode).toBigIntegerExact();
}

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

@Override
public BigInteger bigIntegerValueExact() {
  return bigDecimalValue().toBigIntegerExact();
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
  public <X> BigInteger wrap(X value, WrapperOptions options) {
    if ( value == null ) {
      return null;
    }
    if ( BigInteger.class.isInstance( value ) ) {
      return (BigInteger) value;
    }
    if ( BigDecimal.class.isInstance( value ) ) {
      return ( (BigDecimal) value ).toBigIntegerExact();
    }
    if ( Number.class.isInstance( value ) ) {
      return BigInteger.valueOf( ( (Number) value ).longValue() );
    }
    throw unknownWrap( value.getClass() );
  }
}

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

/**
 * If {@code intVal} has a fractional part throws an exception,
 * otherwise it counts the number of bits of value and checks if it's out of
 * the range of the primitive type. If the number fits in the primitive type
 * returns this number as {@code long}, otherwise throws an
 * exception.
 *
 * @param bitLengthOfType
 *            number of bits of the type whose value will be calculated
 *            exactly
 * @return the exact value of the integer part of {@code BigDecimal}
 *         when is possible
 * @throws ArithmeticException when rounding is necessary or the
 *             number don't fit in the primitive type
 */
private long valueExact(int bitLengthOfType) {
  BigInteger bigInteger = toBigIntegerExact();
  if (bigInteger.bitLength() < bitLengthOfType) {
    // It fits in the primitive type
    return bigInteger.longValue();
  }
  throw new ArithmeticException("Rounding necessary");
}

代码示例来源:origin: hibernate/hibernate-orm

return (X) value.toBigIntegerExact();

代码示例来源:origin: google/guava

@GwtIncompatible // TODO
@AndroidIncompatible // slow
public void testDivNonZero() {
 for (BigInteger p : NONZERO_BIGINTEGER_CANDIDATES) {
  for (BigInteger q : NONZERO_BIGINTEGER_CANDIDATES) {
   for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) {
    BigInteger expected =
      new BigDecimal(p).divide(new BigDecimal(q), 0, mode).toBigIntegerExact();
    assertEquals(expected, BigIntegerMath.divide(p, q, mode));
   }
  }
 }
}

代码示例来源:origin: graphql-java/graphql-java

@Override
  public BigInteger parseLiteral(Object input) {
    if (input instanceof StringValue) {
      try {
        return new BigDecimal(((StringValue) input).getValue()).toBigIntegerExact();
      } catch (NumberFormatException | ArithmeticException e) {
        throw new CoercingParseLiteralException(
            "Unable to turn AST input into a 'BigInteger' : '" + String.valueOf(input) + "'"
        );
      }
    } else if (input instanceof IntValue) {
      return ((IntValue) input).getValue();
    } else if (input instanceof FloatValue) {
      try {
        return ((FloatValue) input).getValue().toBigIntegerExact();
      } catch (ArithmeticException e) {
        throw new CoercingParseLiteralException(
            "Unable to turn AST input into a 'BigInteger' : '" + String.valueOf(input) + "'"
        );
      }
    }
    throw new CoercingParseLiteralException(
        "Expected AST type 'IntValue', 'StringValue' or 'FloatValue' but was '" + typeName(input) + "'."
    );
  }
});

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

bigDec.toBigIntegerExact();
try { return bigDec.intValueExact(); }
catch(ArithmeticException e) {}

代码示例来源:origin: java-json-tools/json-schema-validator

protected final ObjectNode digestedNumberNode(final JsonNode schema)
  {
    final ObjectNode ret = FACTORY.objectNode();

    final JsonNode node = schema.get(keyword);
    final boolean isLong = valueIsLong(node);
    ret.put("valueIsLong", isLong);

    if (isLong) {
      ret.put(keyword, node.canConvertToInt()
        ? FACTORY.numberNode(node.intValue())
        : FACTORY.numberNode(node.longValue()));
      return ret;
    }

    final BigDecimal decimal = node.decimalValue();
    ret.put(keyword, decimal.scale() == 0
      ? FACTORY.numberNode(decimal.toBigIntegerExact())
      : node);

    return ret;
  }
}

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

public ANXWithdrawalResponseWrapper anxWithdrawFunds(
  String currency, BigDecimal amount, String address) throws IOException {
 try {
  ANXWithdrawalResponseWrapper anxWithdrawalResponseWrapper =
    anxV2.withdrawBtc(
      exchange.getExchangeSpecification().getApiKey(),
      signatureCreator,
      exchange.getNonceFactory(),
      currency,
      address,
      amount
        .multiply(new BigDecimal(ANXUtils.BTC_VOLUME_AND_AMOUNT_INT_2_DECIMAL_FACTOR_2))
        .toBigIntegerExact(),
      1,
      false,
      false);
  return anxWithdrawalResponseWrapper;
 } catch (ANXException e) {
  throw handleError(e);
 } catch (HttpStatusIOException e) {
  throw handleHttpError(e);
 }
}

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

public ANXWithdrawalResponseWrapper anxWithdrawFunds(
  String currency, BigDecimal amount, String address, String destinationTag)
  throws IOException {
 try {
  ANXWithdrawalResponseWrapper anxWithdrawalResponseWrapper =
    anxV2.withdrawXrp(
      exchange.getExchangeSpecification().getApiKey(),
      signatureCreator,
      exchange.getNonceFactory(),
      currency,
      address,
      amount
        .multiply(new BigDecimal(ANXUtils.BTC_VOLUME_AND_AMOUNT_INT_2_DECIMAL_FACTOR_2))
        .toBigIntegerExact(),
      1,
      false,
      false,
      destinationTag);
  return anxWithdrawalResponseWrapper;
 } catch (ANXException e) {
  throw handleError(e);
 } catch (HttpStatusIOException e) {
  throw handleHttpError(e);
 }
}

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

private TransactionReceipt send(
    String toAddress, BigDecimal value, Convert.Unit unit, BigInteger gasPrice,
    BigInteger gasLimit) throws IOException, InterruptedException,
    TransactionException {
  BigDecimal weiValue = Convert.toWei(value, unit);
  if (!Numeric.isIntegerValue(weiValue)) {
    throw new UnsupportedOperationException(
        "Non decimal Wei value provided: " + value + " " + unit.toString()
            + " = " + weiValue + " Wei");
  }
  String resolvedAddress = ensResolver.resolve(toAddress);
  return send(resolvedAddress, "", weiValue.toBigIntegerExact(), gasPrice, gasLimit);
}

代码示例来源:origin: eclipse/smarthome

@Override
public String format(String pattern) {
  // The value could be an integer value. Try to convert to BigInteger in
  // order to have access to more conversion formats.
  try {
    return String.format(pattern, value.toBigIntegerExact());
  } catch (ArithmeticException ae) {
    // Could not convert to integer value without loss of
    // information. Fall through to default behavior.
  } catch (IllegalFormatConversionException ifce) {
    // The conversion is not valid for the type BigInteger. This
    // happens, if the format is like "%.1f" but the value is an
    // integer. Fall through to default behavior.
  }
  return String.format(pattern, value);
}

代码示例来源:origin: org.glassfish/javax.json

@Override
public BigInteger bigIntegerValueExact() {
  return bigDecimalValue().toBigIntegerExact();
}

代码示例来源:origin: eclipse/smarthome

@Override
public String format(@Nullable String pattern) {
  String formatPattern = pattern;
  if (formatPattern != null && formatPattern.contains(UnitUtils.UNIT_PLACEHOLDER)) {
    String unitSymbol = getUnit().equals(SmartHomeUnits.PERCENT) ? "%%" : getUnit().toString();
    formatPattern = formatPattern.replace(UnitUtils.UNIT_PLACEHOLDER, unitSymbol);
  }
  // The value could be an integer value. Try to convert to BigInteger in
  // order to have access to more conversion formats.
  try {
    return String.format(formatPattern, toBigDecimal().toBigIntegerExact());
  } catch (ArithmeticException ae) {
    // Could not convert to integer value without loss of
    // information. Fall through to default behavior.
  } catch (IllegalFormatConversionException ifce) {
    // The conversion is not valid for the type BigInteger. This
    // happens, if the format is like "%.1f" but the value is an
    // integer. Fall through to default behavior.
  }
  return String.format(formatPattern, toBigDecimal());
}

相关文章