Refine search
public static int extractNanosecondDecimal(BigDecimal value, long integer) { // !!! 14-Mar-2016, tatu: Somewhat inefficient; should replace with functionally // equivalent code that just subtracts integral part? (or, measure and show // there's no difference and do nothing... ) return value.subtract(new BigDecimal(integer)).multiply(ONE_BILLION).intValue(); } }
@Override protected HiveIntervalDayTime getIntervalDayTime(String arg) { BigDecimal bd = new BigDecimal(arg); BigDecimal bdSeconds = new BigDecimal(bd.toBigInteger()); BigDecimal bdNanos = bd.subtract(bdSeconds); return new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(), bdNanos.multiply(NANOS_PER_SEC_BD).intValue()); } }
/** * 元转分 * @param yuan * @return */ public static Integer Yuan2Fen(Double yuan) { return new BigDecimal(String.valueOf(yuan)).movePointRight(2).intValue(); }
@Override protected HiveIntervalDayTime getIntervalDayTime(String arg) { BigDecimal bd = new BigDecimal(arg); BigDecimal bdSeconds = new BigDecimal(bd.toBigInteger()); BigDecimal bdNanos = bd.subtract(bdSeconds); return new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(), bdNanos.multiply(NANOS_PER_SEC_BD).intValue()); } }
public static int extractNanosecondDecimal(BigDecimal value, long integer) { // !!! 14-Mar-2016, tatu: Somewhat inefficient; should replace with functionally // equivalent code that just subtracts integral part? (or, measure and show // there's no difference and do nothing... ) return value.subtract(new BigDecimal(integer)).multiply(ONE_BILLION).intValue(); } }
new HiveIntervalDayTime(0, 0, Integer.parseInt(intervalString), 0, 0)); case HiveParser.TOK_INTERVAL_SECOND_LITERAL: BigDecimal bd = new BigDecimal(intervalString); BigDecimal bdSeconds = new BigDecimal(bd.toBigInteger()); BigDecimal bdNanos = bd.subtract(bdSeconds); return new ExprNodeConstantDesc(TypeInfoFactory.intervalDayTimeTypeInfo, new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(), bdNanos.multiply(NANOS_PER_SEC_BD).intValue())); default: throw new IllegalArgumentException("Invalid time literal type " + expr.getType());
/** * @Deprecated due to potential unbounded latency on some JRE releases. */ public static int extractNanosecondDecimal(BigDecimal value, long integer) { // !!! 14-Mar-2016, tatu: Somewhat inefficient; should replace with functionally // equivalent code that just subtracts integral part? (or, measure and show // there's no difference and do nothing... ) return value.subtract(new BigDecimal(integer)).multiply(ONE_BILLION).intValue(); }
new HiveIntervalDayTime(0, 0, Integer.parseInt(intervalString), 0, 0)); case HiveParser.TOK_INTERVAL_SECOND_LITERAL: BigDecimal bd = new BigDecimal(intervalString); BigDecimal bdSeconds = new BigDecimal(bd.toBigInteger()); BigDecimal bdNanos = bd.subtract(bdSeconds); return new ExprNodeConstantDesc(TypeInfoFactory.intervalDayTimeTypeInfo, new HiveIntervalDayTime(0, 0, 0, bdSeconds.intValueExact(), bdNanos.multiply(NANOS_PER_SEC_BD).intValue())); default: throw new IllegalArgumentException("Invalid time literal type " + expr.getType());
public static void main(String args[]) { new BigDecimal("1.0"); BigDecimal b = new BigDecimal("2.0"); b.intValue(); "".matches("a"); }
/** * 将double与小数相乘,计算结四舍五入保留整数位。 * * @param num1 数字1 * @param num2 数字2 * @return 数字相乘计算结果 */ public static int multiply(double num1, double num2) { BigDecimal num1Bd = new BigDecimal(Double.toString(num1)); BigDecimal num2Bd = new BigDecimal(Double.toString(num2)); MathContext mathContext = new MathContext(num1Bd.precision(), RoundingMode.HALF_UP); return num1Bd.multiply(num2Bd, mathContext).intValue(); }
/** * 将double与小数相乘,计算结四舍五入保留整数位。 * * @param num1 数字1 * @param num2 数字2 * @return 数字相乘计算结果 */ public static int multiply(double num1, double num2) { BigDecimal num1Bd = new BigDecimal(Double.toString(num1)); BigDecimal num2Bd = new BigDecimal(Double.toString(num2)); MathContext mathContext = new MathContext(num1Bd.precision(), RoundingMode.HALF_UP); return num1Bd.multiply(num2Bd, mathContext).intValue(); }
/** * Round half up. * * @param obj object to be converted * @return rounded half up number */ public static int roundHalfUp(final Object obj) { if (obj instanceof Short) { return (short) obj; } if (obj instanceof Integer) { return (int) obj; } if (obj instanceof Long) { return ((Long) obj).intValue(); } if (obj instanceof Double) { return new BigDecimal((double) obj).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); } if (obj instanceof Float) { return new BigDecimal((float) obj).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); } if (obj instanceof String) { return new BigDecimal((String) obj).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); } throw new ShardingException("Invalid value to transfer: %s", obj); }
public static Timestamp doubleToTimestamp(double f) { try { long seconds = (long) f; // We must ensure the exactness of the double's fractional portion. // 0.6 as the fraction part will be converted to 0.59999... and // significantly reduce the savings from binary serialization BigDecimal bd = new BigDecimal(String.valueOf(f)); bd = bd.subtract(new BigDecimal(seconds)).multiply(new BigDecimal(1000000000)); int nanos = bd.intValue(); // Convert to millis long millis = seconds * 1000; if (nanos < 0) { millis -= 1000; nanos += 1000000000; } Timestamp t = new Timestamp(millis); // Set remaining fractional portion to nanos t.setNanos(nanos); return t; } catch (NumberFormatException nfe) { return null; } catch (IllegalArgumentException iae) { return null; } }
public static Timestamp doubleToTimestamp(double f) { try { long seconds = (long) f; // We must ensure the exactness of the double's fractional portion. // 0.6 as the fraction part will be converted to 0.59999... and // significantly reduce the savings from binary serialization BigDecimal bd = new BigDecimal(String.valueOf(f)); bd = bd.subtract(new BigDecimal(seconds)).multiply(new BigDecimal(1000000000)); int nanos = bd.intValue(); // Convert to millis long millis = seconds * 1000; if (nanos < 0) { millis -= 1000; nanos += 1000000000; } return Timestamp.ofEpochMilli(millis, nanos); } catch (IllegalArgumentException | DateTimeException nfe) { return null; } }
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; }
public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) { if (x.getParameters().size() == 0) { return SQLEvalVisitor.EVAL_ERROR; } StringBuffer buf = new StringBuffer(x.getParameters().size()); for (SQLExpr param : x.getParameters()) { param.accept(visitor); Object paramValue = param.getAttributes().get(EVAL_VALUE); if (paramValue instanceof Number) { int charCode = ((Number) paramValue).intValue(); buf.append((char) charCode); } else if (paramValue instanceof String) { try { int charCode = new BigDecimal((String) paramValue).intValue(); buf.append((char) charCode); } catch (NumberFormatException e) { } } else { return SQLEvalVisitor.EVAL_ERROR; } } return buf.toString(); } }
public static Timestamp decimalToTimestamp(HiveDecimalV1 dec) { try { BigDecimal nanoInstant = dec.bigDecimalValue().multiply(BILLION_BIG_DECIMAL); int nanos = nanoInstant.remainder(BILLION_BIG_DECIMAL).intValue(); if (nanos < 0) { nanos += 1000000000; } long seconds = nanoInstant.subtract(new BigDecimal(nanos)).divide(BILLION_BIG_DECIMAL).longValue(); Timestamp t = new Timestamp(seconds * 1000); t.setNanos(nanos); return t; } catch (NumberFormatException nfe) { return null; } catch (IllegalArgumentException iae) { return null; } }
Integer.parseInt(matcher.group(6))); if (!TextUtils.isEmpty(matcher.group(8))) { final BigDecimal bd = new BigDecimal("0." + matcher.group(8)); dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue());
public static Timestamp decimalToTimestamp(HiveDecimalV1 dec) { try { BigDecimal nanoInstant = dec.bigDecimalValue().multiply(BILLION_BIG_DECIMAL); int nanos = nanoInstant.remainder(BILLION_BIG_DECIMAL).intValue(); if (nanos < 0) { nanos += 1000000000; } long seconds = nanoInstant.subtract(new BigDecimal(nanos)).divide(BILLION_BIG_DECIMAL).longValue(); return Timestamp.ofEpochSecond(seconds, nanos); } catch (IllegalArgumentException | DateTimeException nfe) { return null; } }
BigDecimal bigSeconds = BigDecimal.valueOf(Math.abs(durationInMillis)).divide(new BigDecimal(1000)); int minutes = bigSeconds.intValue() / 60;