@Override public int getDecimalValueAsInt() { BigDecimal d = getDecimal(); if (d != null) { return d.intValueExact(); } return 0; }
/** * Returns <tt>value<sup>exponent</sup></tt>. * * @param value the value * @param exponent the exponent * @return <tt>value<sup>exponent</sup></tt> */ public BigDecimal pow(BigDecimal value, BigDecimal exponent) { try { return pow(value, exponent.intValueExact()); } catch (ArithmeticException overflow) { // exponent isn't an integer, or is too big. Use the imprecise double conversion instead. double result = Math.pow(value.doubleValue(), exponent.doubleValue()); return new BigDecimal(result); } }
private Integer getRealValue(BigDecimal fraction, Integer min, Integer max) { BigDecimal low = BigDecimal.valueOf(min.intValue()); BigDecimal range = BigDecimal.valueOf(max.intValue()).subtract(low).add(BigDecimal.ONE); BigDecimal value = fraction.multiply(range).setScale(0, RoundingMode.FLOOR).add(low); return Integer.valueOf(value.intValueExact()); }
/** Evaluate the result of / computation */ @Override public String divide(String[] args) { double number1, number2, result; number1 = Double.parseDouble(args[0]); number2 = Double.parseDouble(args[1]); result = number1 / number2; BigDecimal resultFormat = new BigDecimal(result); try { return "" + resultFormat.intValueExact(); } catch (ArithmeticException e) { return "" + result; } }
private Object handlePrimitive(JsonPrimitive json) { if (json.isBoolean()) return json.getAsBoolean(); else if (json.isString()) return json.getAsString(); else { BigDecimal bigDec = json.getAsBigDecimal(); // Find out if it is an int type try { bigDec.toBigIntegerExact(); try { return bigDec.intValueExact(); } catch (ArithmeticException e) { } return bigDec.longValue(); } catch (ArithmeticException e) { } // Just return it as a double return bigDec.doubleValue(); } }
/** * Returns the integer value of this literal. * * @param exact Whether the value has to be exact. If true, and the literal is a fraction (e.g. * 3.14), throws. If false, discards the fractional part of the value. * @return Integer value of this literal */ public int intValue(boolean exact) { switch (typeName) { case DECIMAL: case DOUBLE: BigDecimal bd = (BigDecimal) value; if (exact) { try { return bd.intValueExact(); } catch (ArithmeticException e) { throw SqlUtil.newContextException( getParserPosition(), RESOURCE.numberLiteralOutOfRange(bd.toString())); } } else { return bd.intValue(); } default: throw Util.unexpected(typeName); } }
/** * Converts a {@link BigDecimal} value into the requested return type if possible. * * @param value the value * @param returnType the class of the returned value; it must be one of {@link BigDecimal}, {@link * Double}, {@link Float}, {@link BigInteger}, {@link Long}, {@link Integer}, {@link Short}, * or {@link Byte} * @return the converted value * @throws IllegalArgumentException if the conversion is not possible or would lead to loss of * data * @throws ClassCastException if the return type is not allowed */ protected static <T> T convertDecimal(final BigDecimal value, final Class<T> returnType) throws IllegalArgumentException, ClassCastException { if (returnType.isAssignableFrom(BigDecimal.class)) { return returnType.cast(value); } else if (returnType.isAssignableFrom(Double.class)) { final double doubleValue = value.doubleValue(); if (BigDecimal.valueOf(doubleValue).compareTo(value) == 0) { return returnType.cast(doubleValue); } else { throw new IllegalArgumentException(); } } else if (returnType.isAssignableFrom(Float.class)) { final Float floatValue = value.floatValue(); if (BigDecimal.valueOf(floatValue).compareTo(value) == 0) { return returnType.cast(floatValue); } else { throw new IllegalArgumentException(); } } else { try { if (returnType.isAssignableFrom(BigInteger.class)) { return returnType.cast(value.toBigIntegerExact()); } else if (returnType.isAssignableFrom(Long.class)) { return returnType.cast(value.longValueExact()); } else if (returnType.isAssignableFrom(Integer.class)) { return returnType.cast(value.intValueExact()); } else if (returnType.isAssignableFrom(Short.class)) { return returnType.cast(value.shortValueExact()); } else if (returnType.isAssignableFrom(Byte.class)) { return returnType.cast(value.byteValueExact()); } else { throw new ClassCastException("unsupported return type " + returnType.getSimpleName()); } } catch (final ArithmeticException e) { throw new IllegalArgumentException(e); } } }
/** * Type converts values to other classes. For example an Integer can be converted to a Long. * * @param <T> Data Type. * @param value The value to be converted. * @param to The class to be converted to. Must be one of the Java types corresponding to the * DataTypes. * @return The converted value. * @throws TypeMismatchException Thrown desired class is incompatible with the source class. * @throws OverflowException Thrown only on narrowing value conversions, e.g from a BigInteger to * an Integer. */ @Nullable public static <T> T convert(Object value, Class<?> to) throws TypeMismatchException, OverflowException { final Object result; if (value == null || value.getClass() == to) { result = value; } else { final Class<?> from = value.getClass(); try { if (from == Integer.class) { // Integer -> ... if (to == Long.class) { result = new Long(((Number) value).longValue()); } else if (to == BigInteger.class) { result = BigInteger.valueOf(((Number) value).intValue()); } else if (to == Float.class) { result = new Float(((Number) value).floatValue()); } else if (to == Double.class) { result = new Double(((Number) value).doubleValue()); } else if (to == BigDecimal.class) { // Use intValue() to avoid precision errors result = new BigDecimal(((Number) value).intValue()); } else { throw new TypeMismatchException(value.getClass(), to); } } else if (from == Long.class) { // Long -> ... if (to == Integer.class) { final long l = ((Long) value).longValue(); if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) { throw new OverflowException((Number) value, to); } result = new Integer((int) l); } else if (to == BigInteger.class) { result = BigInteger.valueOf(((Number) value).longValue()); } else if (to == Float.class) { result = new Float(((Number) value).floatValue()); } else if (to == Double.class) { result = new Double(((Number) value).doubleValue()); } else if (to == BigDecimal.class) { // Use longValue() to avoid precision errors result = new BigDecimal(((Number) value).longValue()); } else { throw new TypeMismatchException(value.getClass(), to); } } else if (from == BigInteger.class) { // BigInteger -> ... final BigInteger bi = (BigInteger) value; if (to == Integer.class) { result = new Integer(bi.intValueExact()); } else if (to == Long.class) { result = new Long(bi.longValueExact()); } else if (to == Float.class) { final float f1 = bi.floatValue(); if (f1 == Float.NEGATIVE_INFINITY || f1 == Float.POSITIVE_INFINITY) { throw new OverflowException(bi, to); } result = new Float(f1); } else if (to == Double.class) { final double d = bi.doubleValue(); if (d == Double.NEGATIVE_INFINITY || d == Double.POSITIVE_INFINITY) { throw new OverflowException(bi, to); } result = new Double(d); } else if (to == BigDecimal.class) { result = new BigDecimal(bi); } else { throw new TypeMismatchException(value.getClass(), to); } } else if (from == Float.class) { // Float -> ... final float fl = ((Float) value).floatValue(); if (to == Integer.class) { if (fl < Integer.MIN_VALUE || fl > Integer.MAX_VALUE | fl % 1 > 0) { throw new OverflowException((Number) value, to); } result = new Integer((int) fl); } else if (to == Long.class) { if (fl < Long.MIN_VALUE || fl > Long.MAX_VALUE | fl % 1 > 0) { throw new OverflowException((Number) value, to); } result = new Long((long) fl); } else if (to == BigInteger.class) { if (fl % 1 > 0) { throw new OverflowException((Number) value, to); } final BigDecimal bd = BigDecimal.valueOf(fl); result = bd.toBigInteger(); } else if (to == Double.class) { result = new Double(((Number) value).doubleValue()); } else if (to == BigDecimal.class) { result = BigDecimal.valueOf(fl); } else { throw new TypeMismatchException(value.getClass(), to); } } else if (from == Double.class) { // Double -> ... final double d = ((Double) value).doubleValue(); if (to == Integer.class) { if (d < Integer.MIN_VALUE || d > Integer.MAX_VALUE || d % 1 > 0) { throw new OverflowException((Number) value, to); } result = new Integer((int) d); } else if (to == Long.class) { // OK if (d < Long.MIN_VALUE || d > Long.MAX_VALUE || d % 1 > 0) { throw new OverflowException((Number) value, to); } result = new Long((int) d); } else if (to == BigInteger.class) { // OK if (d % 1 > 0) { throw new OverflowException((Number) value, to); } final BigDecimal bd = BigDecimal.valueOf(d); result = bd.toBigInteger(); } else if (to == Float.class) { // OK if (d < -Float.MAX_VALUE || d > Float.MAX_VALUE) { throw new OverflowException((Number) value, to); } result = new Float((float) d); } else if (to == BigDecimal.class) { // OK result = BigDecimal.valueOf(d); } else { throw new TypeMismatchException(value.getClass(), to); } } else if (from == BigDecimal.class) { // BigDecimal -> ... final BigDecimal bd = (BigDecimal) value; if (to == Integer.class) { // OK result = new Integer(bd.intValueExact()); } else if (to == Long.class) { // OK result = new Long(bd.longValueExact()); } else if (to == BigInteger.class) { // OK // BigDecimal modulus final BigDecimal remainder = bd.remainder(BigDecimal.ONE); if (!remainder.equals(BigDecimal.ZERO)) { throw new OverflowException(bd, to); } result = bd.toBigInteger(); } else if (to == Float.class) { // OK if (bd.compareTo(BigDecimal_MIN_FLOAT) < 0 || bd.compareTo(BigDecimal_MAX_FLOAT) > 0) { throw new OverflowException(bd, to); } result = new Float(bd.floatValue()); } else if (to == Double.class) { // OK if (bd.compareTo(BigDecimal_MIN_DOUBLE) < 0 || bd.compareTo(BigDecimal_MAX_DOUBLE) > 0) { throw new OverflowException(bd, to); } result = new Double(bd.doubleValue()); } else { throw new TypeMismatchException(value.getClass(), to); } } else { throw new UnexpectedException("convert: " + from.getName()); } } catch (final ArithmeticException e) { // Thrown by intValueExact() etc. throw new OverflowException((Number) value, to); } } @SuppressWarnings("unchecked") final T t = (T) result; return t; }