List of usage examples for java.math BigDecimal toBigInteger
public BigInteger toBigInteger()
From source file:Main.java
public static void main(String[] args) { BigDecimal bg1 = new BigDecimal("235.738"); BigInteger i1 = bg1.toBigInteger(); System.out.println("BigInteger value of " + bg1 + " is " + i1); }
From source file:it.geosolutions.geobatch.destination.common.utils.FeatureLoaderUtils.java
/** * Load all the values from a given BigDecimal attribute and return a list of Double * //from w w w. j a v a 2s .c o m * <b>The result is cached</b> * * @param datastore * @param featureTypeName * @param attribute * @param forceLoading * @return */ public static List<Double> loadFeatureAttributesInt(DataStore datastore, String featureTypeName, String attribute, boolean forceLoading) { List<BigDecimal> list = loadFeatureAttributesInternal(datastore, featureTypeName, attribute, forceLoading); List<Double> resultList = new ArrayList<Double>(); for (BigDecimal el : list) { resultList.add(el.toBigInteger().doubleValue()); } return resultList; }
From source file:edu.macalester.tagrelatedness.KendallsCorrelation.java
private static BigDecimal getInitialApproximation(BigDecimal n) { BigInteger integerPart = n.toBigInteger(); int length = integerPart.toString().length(); if ((length % 2) == 0) { length--;/*from ww w . ja va2 s . c o m*/ } length /= 2; BigDecimal guess = ONE.movePointRight(length); return guess; }
From source file:com.nearinfinity.honeycomb.hbase.bulkload.FieldParser.java
private static ByteBuffer extractDecimal(String val, int precision, int right_scale) { int left_scale = precision - 2; BigDecimal x = new BigDecimal(val); boolean is_negative = x.compareTo(BigDecimal.ZERO) == -1; x = x.abs();/*w w w. j av a2 s. c o m*/ BigDecimal left = x.setScale(0, RoundingMode.DOWN); BigDecimal right = x.subtract(left).movePointRight(right_scale); int right_bytes_len = bytesFromDigits(right_scale); int left_bytes_len = bytesFromDigits(left_scale); byte[] left_bytes = left.toBigInteger().toByteArray(); byte[] right_bytes = right.toBigInteger().toByteArray(); // Bit twiddling is fun byte[] buff = new byte[left_bytes_len + right_bytes_len]; System.arraycopy(left_bytes, 0, buff, left_bytes_len - left_bytes.length, left_bytes.length); System.arraycopy(right_bytes, 0, buff, right_bytes_len - right_bytes.length + left_bytes_len, right_bytes.length); buff[0] ^= -128; // Flip first bit, 0x80 if (is_negative) { // Flip all bits for (int i = 0; i < buff.length; i++) { buff[i] ^= -1; // 0xff } } return ByteBuffer.wrap(buff); }
From source file:Main.java
/** * Return just the fractional part of the seconds component of a duration object, * i.e. 1:2:34.45 => 0.45//from w w w . j a va2s. c o m * @param duration The duration object to examine. * @return The fractional part of the seconds field. */ private static BigDecimal fractionalSeconds(Duration duration) { BigDecimal seconds = (BigDecimal) duration.getField(DatatypeConstants.SECONDS); return seconds.subtract(new BigDecimal(seconds.toBigInteger())); }
From source file:org.eel.kitchen.jsonschema.GsonProvider.java
private static JsonNode toNumberNode(final BigDecimal decimal) { try {//w ww. j av a 2 s .c o m return factory.numberNode(decimal.intValueExact()); } catch (ArithmeticException ignored) { try { return factory.numberNode(decimal.longValueExact()); } catch (ArithmeticException ignoredAgain) { return decimal.scale() == 0 ? factory.numberNode(decimal.toBigInteger()) : factory.numberNode(decimal); } } }
From source file:com.github.jessemull.microflex.util.BigDecimalUtil.java
/** * Converts a list of BigDecimals to a a list of BigIntegers. * @param List<BigDecimal> list of BigDecimals * @return list of BigIntegers *///w w w.j av a 2 s. c om public static List<BigInteger> toBigIntList(List<BigDecimal> list) { List<BigInteger> toBigInt = new ArrayList<BigInteger>(); for (BigDecimal bd : list) { toBigInt.add(bd.toBigInteger()); } return toBigInt; }
From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java
public static RowMapperFactory getScalarMapper(final Type itemType, final int idx, boolean req) { if (itemType == String.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getString(idx); }//from w w w.j a v a 2s. com }; } if (itemType == Integer.class || itemType == Integer.TYPE) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getInt(idx); } }; } if (itemType == URL.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getURL(idx); } }; } if (itemType == BigInteger.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { final BigDecimal res = rs.getBigDecimal(idx); return res == null ? null : res.toBigInteger(); } }; } if (itemType == BigDecimal.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBigDecimal(idx); } }; } if (itemType == InputStream.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBinaryStream(idx); } }; } if (itemType == Boolean.class || itemType == Boolean.TYPE) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBoolean(idx); } }; } if (itemType == Blob.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBlob(idx); } }; } if (itemType == java.sql.Date.class || itemType == java.util.Date.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getTimestamp(idx); } }; } if (itemType instanceof Class) { final Class itemClass = (Class) itemType; final ColumnMapper columnMapper = ColumnMapperRegistry.lookup(itemClass); if (columnMapper != null) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return columnMapper.map(rs, idx); } }; } final Converter converter = ConvertUtils.lookup(itemClass); if (converter != null) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { String s = rs.getString(idx); if (s == null) { return null; } return converter.convert(itemClass, s); } }; } if (Enum.class.isAssignableFrom((Class<?>) itemType)) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { String s = rs.getString(idx); if (s == null) { return null; } //noinspection unchecked return Enum.valueOf((Class<Enum>) itemType, s); } }; } } if (req) { throw new IllegalArgumentException("no mapping defined for " + itemType); } return null; }
From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java
public static RowMapperFactory getScalarRowMapperFactory(final Type itemType, final String name, boolean req) { if (itemType == String.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getString(name); }/*w w w . j a v a2 s .co m*/ }; } if (itemType == Integer.class || itemType == Integer.TYPE) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getInt(name); } }; } if (itemType == URL.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getURL(name); } }; } if (itemType == BigInteger.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { final BigDecimal res = rs.getBigDecimal(name); return res == null ? null : res.toBigInteger(); } }; } if (itemType == BigDecimal.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBigDecimal(name); } }; } if (itemType == InputStream.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBinaryStream(name); } }; } if (itemType == Boolean.class || itemType == Boolean.TYPE) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBoolean(name); } }; } if (itemType == Blob.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBlob(name); } }; } if (itemType == java.sql.Date.class || itemType == java.util.Date.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getTimestamp(name); } }; } if (itemType instanceof Class) { final Class itemClass = (Class) itemType; final ColumnMapper columnMapper = ColumnMapperRegistry.lookup(itemClass); if (columnMapper != null) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return columnMapper.map(rs, name); } }; } final Converter converter = ConvertUtils.lookup(itemClass); if (converter != null) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { String s = rs.getString(name); if (s == null) { return null; } return converter.convert(itemClass, s); } }; } if (Enum.class.isAssignableFrom((Class<?>) itemType)) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { String s = rs.getString(name); if (s == null) { return null; } //noinspection unchecked return Enum.valueOf((Class<Enum>) itemType, s); } }; } } if (req) { throw new IllegalArgumentException("no mapping defined for " + itemType); } return null; }
From source file:org.apache.hadoop.hive.common.type.HiveIntervalDayTime.java
public static HiveIntervalDayTime valueOf(String strVal) { HiveIntervalDayTime result = null;/*from w w w. j a v a 2s.co m*/ if (strVal == null) { throw new IllegalArgumentException("Interval day-time string was null"); } Matcher patternMatcher = PATTERN_MATCHER.get(); patternMatcher.reset(strVal); if (patternMatcher.matches()) { // Parse out the individual parts try { // Sign - whether interval is positive or negative int sign = 1; String field = patternMatcher.group(1); if (field != null && field.equals("-")) { sign = -1; } int days = sign * IntervalDayTimeUtils.parseNumericValueWithRange("day", patternMatcher.group(2), 0, Integer.MAX_VALUE); byte hours = (byte) (sign * IntervalDayTimeUtils.parseNumericValueWithRange("hour", patternMatcher.group(3), 0, 23)); byte minutes = (byte) (sign * IntervalDayTimeUtils.parseNumericValueWithRange("minute", patternMatcher.group(4), 0, 59)); int seconds = 0; int nanos = 0; field = patternMatcher.group(5); if (field != null) { BigDecimal bdSeconds = new BigDecimal(field); if (bdSeconds.compareTo(IntervalDayTimeUtils.MAX_INT_BD) > 0) { throw new IllegalArgumentException("seconds value of " + bdSeconds + " too large"); } seconds = sign * bdSeconds.intValue(); nanos = sign * bdSeconds.subtract(new BigDecimal(bdSeconds.toBigInteger())) .multiply(IntervalDayTimeUtils.NANOS_PER_SEC_BD).intValue(); } result = new HiveIntervalDayTime(days, hours, minutes, seconds, nanos); } catch (Exception err) { throw new IllegalArgumentException("Error parsing interval day-time string: " + strVal, err); } } else { throw new IllegalArgumentException( "Interval string does not match day-time format of 'd h:m:s.n': " + strVal); } return result; }