/** * 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(); }
private static Optional<BigInteger> asBigInteger(BigDecimal v) { try { return Optional.of(v.toBigIntegerExact()); } catch (ArithmeticException e) { return Optional.empty(); } }
/** * 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(); }
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; }
/** * 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(); }
/** * 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(); }
@Override public BigInteger bigIntegerValueExact() { return bigDecimalValue().toBigIntegerExact(); }
@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() ); } }
/** * 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"); }
return (X) value.toBigIntegerExact();
@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)); } } } }
@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) + "'." ); } });
bigDec.toBigIntegerExact(); try { return bigDec.intValueExact(); } catch(ArithmeticException e) {}
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; } }
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); } }
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); } }
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); }
@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); }
@Override public BigInteger bigIntegerValueExact() { return bigDecimalValue().toBigIntegerExact(); }
@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()); }