List of usage examples for java.math BigDecimal toBigInteger
public BigInteger toBigInteger()
From source file:org.hx.rainbow.common.util.JavaBeanUtil.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private static void changeObject(Map<String, Object> map, String key, Object value, Class clazz, String dataFormat) throws ParseException { if (value instanceof String) { String valueStr = (String) value; if (clazz.isEnum()) { map.put(key, Enum.valueOf(clazz, valueStr)); } else if (clazz == Date.class) { SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DEFAULT_DATE_PATTERN); Date date = sdf.parse(valueStr); map.put(key, date);/*from w ww.ja va 2 s.com*/ } else if (clazz == Integer.class) { map.put(key, Integer.valueOf(valueStr)); } else if (clazz == BigDecimal.class) { map.put(key, new BigDecimal(valueStr)); } else if (clazz == Boolean.class) { map.put(key, new Boolean(valueStr)); } else if (clazz == Number.class) { map.put(key, new Integer(valueStr)); } else if (clazz == int.class) { map.put(key, Integer.parseInt(valueStr)); } else { map.put(key, valueStr); } } else if (value instanceof Integer) { Integer valueInt = (Integer) value; if (clazz == String.class) { map.put(key, valueInt.toString()); } else if (clazz == Date.class) { map.put(key, new Date(valueInt)); } else { map.put(key, valueInt); } } else if (value instanceof Boolean) { Boolean valueBoolean = (Boolean) value; if (clazz == String.class) { map.put(key, valueBoolean.toString()); } else { map.put(key, valueBoolean); } } else if (value instanceof Date) { Date valueDate = (Date) value; if (clazz == String.class) { SimpleDateFormat sdf = new SimpleDateFormat(dataFormat); map.put(key, sdf.format(valueDate)); } else { map.put(key, valueDate); } } else if (value instanceof BigDecimal) { BigDecimal valueBigDecimal = (BigDecimal) value; if (clazz == String.class) { map.put(key, valueBigDecimal.toPlainString()); } else if (clazz == Integer.class) { map.put(key, valueBigDecimal.toBigInteger()); } else { map.put(key, valueBigDecimal); } } else { map.put(key, value); } }
From source file:com.aegiswallet.utils.WalletUtils.java
public static BigInteger btcValue(Context context, SharedPreferences prefs, @Nonnull final BigInteger localValue) { BigInteger result = null;//from www . j a v a 2s. c o m BigDecimal exchangeRate = getExchangeRate(context, prefs); if (exchangeRate != null) result = localValue.multiply(ONE_BTC).divide(exchangeRate.toBigInteger()); return result; }
From source file:org.multibit.utils.CSMiscUtils.java
@Deprecated public static BigInteger calcRawPercentageCharge(CSAsset asset, BigInteger transferAmount) { CoinSparkGenesis genesis = asset.getGenesis(); int chargeBasisPoints = genesis.getChargeBasisPoints(); if (chargeBasisPoints == 0) return BigInteger.ZERO; BigDecimal d = new BigDecimal(chargeBasisPoints); d = d.movePointLeft(2 + 2); // 2 to get whole percent number, another 2 to get fraction d = d.multiply(new BigDecimal(transferAmount), MathContext.UNLIMITED); return d.toBigInteger(); }
From source file:jp.terasoluna.fw.validation.ValidationUtil.java
/** * ??????????//from w ww . jav a 2 s . co m * <br> * ?????????????? * <ul> * <li>??? * <ol> * <li><code>isAccordedInteger</code>?<code>true</code>??? * ????<code>integerLength</code>?? * ???????? * * <li><code>isAccordedInteger</code>?<code>false</code>??? * ????<code>integerLength</code>????? * ?? * </ol> * * <li>???? * <ol> * <li><code>isAccordedScale</code>?<code>true</code>??? * ?????<code>scaleLength</code>?? * ???????? * * <li><code>isAccordedScale</code>?<code>true</code>??? * ?????<code>scaleLength</code>????? * ?? * </ol> * </ul> * * @param value * @param integerLength ?? * @param isAccordedInteger * ?????? * <code>true</code>? * ?????? * <code>false</code>? * @param scaleLength ??? * @param isAccordedScale * ??????? * <code>true</code>? * ??????? * <code>false</code>? * * @return * ???????? * <code>true</code>? * ?????<code>false</code>? */ public static boolean isNumber(BigDecimal value, int integerLength, boolean isAccordedInteger, int scaleLength, boolean isAccordedScale) { // ?null??true? if (value == null) { return true; } // ?? // ?? BigInteger bi = value.toBigInteger().abs(); // ? int length = bi.toString().length(); if (!checkNumberFigures(length, integerLength, isAccordedInteger)) { return false; } // ??? int scale = value.scale(); if (!checkNumberFigures(scale, scaleLength, isAccordedScale)) { return false; } return true; }
From source file:org.osiam.resource_server.storage.helper.NumberPadder.java
/** * Removes the offset and padding from a number * * @param value/*from w w w.j a va 2 s . c o m*/ * the padded number as {@link String} * @return the number decreased by offset and leading '0's removed */ public String unpad(String value) { BigDecimal decimalValue = new BigDecimal(value); BigDecimal returnDecimalValue = new BigDecimal(decimalValue.toBigInteger().subtract(BIG_OFFSET)); BigDecimal signum = new BigDecimal(returnDecimalValue.signum()); BigDecimal fractionalPart = decimalValue.remainder(BigDecimal.ONE).multiply(signum); returnDecimalValue = returnDecimalValue.add(fractionalPart); return returnDecimalValue.toString(); }
From source file:io.curly.advisor.model.Review.java
private BigDecimal fixPrecision(BigDecimal rate) { if (rate == null) return BigDecimal.ZERO; BigDecimal decimal = rate.remainder(BigDecimal.ONE); BigInteger integer = rate.toBigInteger(); if (decimal.compareTo(new BigDecimal(0.5)) > 0) { return new BigDecimal(integer.add(new BigInteger("1"))); } else if (decimal.compareTo(new BigDecimal(0.5)) < 0) { return new BigDecimal(integer); }/*from w ww . java 2 s . c om*/ return rate; }
From source file:com.thinkbiganalytics.spark.validation.HCatDataType.java
private HCatDataType(Class clazz) { this.convertibleType = clazz; if (clazz == Date.class || clazz == Timestamp.class || clazz == byte[].class) { this.isnumeric = false; } else {// w ww . j a v a 2s . co m this.isnumeric = true; BigDecimal minDecimal = new BigDecimal(Long.MIN_VALUE); BigDecimal maxDecimal = new BigDecimal(Long.MAX_VALUE); if (clazz == BigInteger.class) { this.min = minDecimal.toBigInteger(); this.max = maxDecimal.toBigInteger(); } else if (clazz == BigDecimal.class) { this.min = null; this.max = null; } else { throw new RuntimeException("Invalid class for constructor " + clazz.getCanonicalName()); } } }
From source file:org.openhab.persistence.influxdb.internal.InfluxDBPersistenceService.java
/** * This method returns an integer if possible if not a double is returned. This is an optimization * for influxdb because integers have less overhead. * // ww w .j av a 2s . com * @param value the BigDecimal to be converted * @return A double if possible else a double is returned. */ private Object convertBigDecimalToNum(BigDecimal value) { Object convertedValue; if (value.scale() == 0) { logger.trace("found no fractional part"); convertedValue = value.toBigInteger(); } else { logger.trace("found fractional part"); convertedValue = value.doubleValue(); } return convertedValue; }
From source file:org.collectionspace.chain.csp.webui.nuispec.DataGenerator.java
/** * pick a random valid date between the defaults defined * @return/* w ww .j a va 2s . c om*/ */ private Date randomDate() { BigDecimal retValue = null; BigDecimal length = maxValue.subtract(minValue); BigDecimal factor = new BigDecimal(random.nextDouble()); retValue = length.multiply(factor).add(minValue); return new Date(retValue.toBigInteger().longValue()); }
From source file:org.geowebcache.diskquota.jdbc.JDBCQuotaStore.java
protected Quota getUsedQuotaByTileSetIdInternal(final String tileSetId) { String sql = dialect.getUsedQuotaByTileSetId(schema, "key"); return jt.queryForOptionalObject(sql, new ParameterizedRowMapper<Quota>() { public Quota mapRow(ResultSet rs, int rowNum) throws SQLException { BigDecimal bytes = rs.getBigDecimal(1); Quota quota = new Quota(bytes.toBigInteger()); quota.setTileSetId(tileSetId); return quota; }//from w w w. jav a 2s . c om }, Collections.singletonMap("key", tileSetId)); }