Example usage for java.math BigDecimal doubleValue

List of usage examples for java.math BigDecimal doubleValue

Introduction

In this page you can find the example usage for java.math BigDecimal doubleValue.

Prototype

@Override
public double doubleValue() 

Source Link

Document

Converts this BigDecimal to a double .

Usage

From source file:com.vmware.bdd.cli.commands.CommandsUtils.java

/**
 * Take the accuracy of double data./* ww  w.j  a v a 2 s  .  c  o m*/
 * <p>
 * For example: <br>
 * A double value = 100.345678; <br>
 * The Double ret = round (value, 4, BigDecimal.ROUND_HALF_UP); <br>
 * "Ret 100.3457 <br>
 *
 * @param value
 *           Double data value. @param scale Precision digits (reserve of
 *           decimal digits).
 * @param roundingMode
 *           Precision value way.
 * @return Precision calculation of data.
 */
private static double round(double value, int scale, int roundingMode) {
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(scale, roundingMode);
    double d = bd.doubleValue();
    return d;
}

From source file:com.almunt.jgcaap.systemupdater.MainActivity.java

public static double round(double value, int places) {
    if (places < 0)
        throw new IllegalArgumentException();
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:CSVWriter.java

private static String getColumnValue(ResultSet rs, int colType, int colIndex)
    throws SQLException, IOException {

  String value = "";
      //www . j a v  a  2  s . c o m
switch (colType)
{
  case Types.BIT:
    Object bit = rs.getObject(colIndex);
    if (bit != null) {
      value = String.valueOf(bit);
    }
  break;
  case Types.BOOLEAN:
    boolean b = rs.getBoolean(colIndex);
    if (!rs.wasNull()) {
      value = Boolean.valueOf(b).toString();
    }
  break;
  case Types.CLOB:
    Clob c = rs.getClob(colIndex);
    if (c != null) {
      value = read(c);
    }
  break;
  case Types.BIGINT:
  case Types.DECIMAL:
  case Types.DOUBLE:
  case Types.FLOAT:
  case Types.REAL:
  case Types.NUMERIC:
    BigDecimal bd = rs.getBigDecimal(colIndex);
    if (bd != null) {
      value = "" + bd.doubleValue();
    }
  break;
  case Types.INTEGER:
  case Types.TINYINT:
  case Types.SMALLINT:
    int intValue = rs.getInt(colIndex);
    if (!rs.wasNull()) {
      value = "" + intValue;
    }
  break;
  case Types.JAVA_OBJECT:
    Object obj = rs.getObject(colIndex);
    if (obj != null) {
      value = String.valueOf(obj);
    }
  break;
  case Types.DATE:
    java.sql.Date date = rs.getDate(colIndex);
    if (date != null) {
      value = DATE_FORMATTER.format(date);;
    }
  break;
  case Types.TIME:
    Time t = rs.getTime(colIndex);
    if (t != null) {
      value = t.toString();
    }
  break;
  case Types.TIMESTAMP:
    Timestamp tstamp = rs.getTimestamp(colIndex);
    if (tstamp != null) {
      value = TIMESTAMP_FORMATTER.format(tstamp);
    }
  break;
  case Types.LONGVARCHAR:
  case Types.VARCHAR:
  case Types.CHAR:
    value = rs.getString(colIndex);
  break;
  default:
    value = "";
}

    
if (value == null)
{
  value = "";
}
    
return value;
      
}

From source file:com.github.jessemull.microflex.util.DoubleUtil.java

/**
 * Safely converts an object to a double. Loss of precision may occur. Throws
 * an arithmetic exception upon overflow.
 * @param    Object    object to parse/*from   ww  w. ja  va  2s  .c  om*/
 * @return             parsed object
 * @throws   ArithmeticException    on overflow
 */
public static double toDouble(Object obj) {

    /* Switch on class and convert to double */

    String type = obj.getClass().getSimpleName();
    double parsed;

    switch (type) {

    case "Byte":
        Byte by = (Byte) obj;
        parsed = by.doubleValue();
        break;

    case "Short":
        Short sh = (Short) obj;
        parsed = sh.doubleValue();
        break;

    case "Integer":
        Integer in = (Integer) obj;
        parsed = in.doubleValue();
        break;

    case "Long":
        Long lo = (Long) obj;
        parsed = lo.doubleValue();
        break;

    case "Float":
        Float fl = (Float) obj;
        parsed = fl.doubleValue();
        break;

    case "BigInteger":
        BigInteger bi = (BigInteger) obj;
        if (!OverFlowUtil.doubleOverflow(bi)) {
            throw new ArithmeticException("Overflow casting " + obj + " to a double.");
        }
        parsed = bi.doubleValue();
        break;

    case "BigDecimal":
        BigDecimal bd = (BigDecimal) obj;
        if (!OverFlowUtil.doubleOverflow(bd)) {
            throw new ArithmeticException("Overflow casting " + obj + " to a double.");
        }
        parsed = bd.doubleValue();
        break;

    case "Double":
        Double db = (Double) obj;
        parsed = db.doubleValue();
        break;

    default:
        throw new IllegalArgumentException(
                "Invalid type: " + type + "\nData values " + "must extend the abstract Number class.");

    }

    return parsed;
}

From source file:Main.java

/** Returns Map with total, squaredTotal, count, average, stdDev, maximum; fieldName field in Maps must have type BigDecimal;
 * if count of non-null fields is less than 2 returns null as cannot calculate a standard deviation */
public static Map<String, BigDecimal> stdDevMaxFromMapField(List<Map<String, Object>> dataList,
        String fieldName, BigDecimal stdDevMultiplier) {
    BigDecimal total = BigDecimal.ZERO;
    BigDecimal squaredTotal = BigDecimal.ZERO;
    int count = 0;
    for (Map<String, Object> dataMap : dataList) {
        if (dataMap == null)
            continue;
        BigDecimal value = (BigDecimal) dataMap.get(fieldName);
        if (value == null)
            continue;
        total = total.add(value);//from w  ww . j  a  va  2  s.co  m
        squaredTotal = squaredTotal.add(value.multiply(value));
        count++;
    }
    if (count < 2)
        return null;

    BigDecimal countBd = new BigDecimal(count);
    BigDecimal average = total.divide(countBd, BigDecimal.ROUND_HALF_UP);
    double totalDouble = total.doubleValue();
    BigDecimal stdDev = new BigDecimal(Math
            .sqrt(Math.abs(squaredTotal.doubleValue() - ((totalDouble * totalDouble) / count)) / (count - 1)));

    Map<String, BigDecimal> retMap = new HashMap<>(6);
    retMap.put("total", total);
    retMap.put("squaredTotal", squaredTotal);
    retMap.put("count", countBd);
    retMap.put("average", average);
    retMap.put("stdDev", stdDev);

    if (stdDevMultiplier != null)
        retMap.put("maximum", average.add(stdDev.multiply(stdDevMultiplier)));

    return retMap;
}

From source file:com.github.jessemull.microflex.util.DoubleUtil.java

/**
 * Safely converts a number to a double. Loss of precision may occur. Throws
 * an arithmetic exception upon overflow.
 * @param    Number    number to parse/*from   www.ja  v a 2s .  co m*/
 * @return             parsed number
 * @throws   ArithmeticException    on overflow
 */
public static double toDouble(Number number) {

    /* Switch on class and convert to double */

    String type = number.getClass().getSimpleName();
    double parsed;

    switch (type) {

    case "Byte":
        Byte by = (Byte) number;
        parsed = by.doubleValue();
        break;

    case "Short":
        Short sh = (Short) number;
        parsed = sh.doubleValue();
        break;

    case "Integer":
        Integer in = (Integer) number;
        parsed = in.doubleValue();
        break;

    case "Long":
        Long lo = (Long) number;
        parsed = lo.doubleValue();
        break;

    case "Float":
        Float fl = (Float) number;
        parsed = fl.doubleValue();
        break;

    case "BigInteger":
        BigInteger bi = (BigInteger) number;
        if (!OverFlowUtil.doubleOverflow(bi)) {
            throw new ArithmeticException("Overflow casting " + number + " to a double.");
        }
        parsed = bi.doubleValue();
        break;

    case "BigDecimal":
        BigDecimal bd = (BigDecimal) number;
        if (!OverFlowUtil.doubleOverflow(bd)) {
            throw new ArithmeticException("Overflow casting " + number + " to a double.");
        }
        parsed = bd.doubleValue();
        break;

    case "Double":
        Double db = (Double) number;
        parsed = db.doubleValue();
        break;

    default:
        throw new IllegalArgumentException(
                "Invalid type: " + type + "\nData values " + "must extend the abstract Number class.");

    }

    return parsed;
}

From source file:ch.algotrader.option.OptionSymbol.java

/**
 * Generates the RIC for the specified {@link ch.algotrader.entity.security.OptionFamily}.
 *///w  ww . jav a  2 s  . c  o m
public static String getRic(OptionFamily family, LocalDate expiration, OptionType type, BigDecimal strike) {

    StringBuilder buffer = new StringBuilder();
    buffer.append(family.getRicRoot() != null ? family.getRicRoot() : family.getSymbolRoot());
    if (OptionType.CALL.equals(type)) {
        buffer.append(monthCallEnc[expiration.getMonthValue() - 1]);
    } else {
        buffer.append(monthPutEnc[expiration.getMonthValue() - 1]);
    }
    buffer.append(DateTimePatterns.DAY_OF_MONTH.format(expiration));
    final String s = DateTimePatterns.YEAR_4_DIGIT.format(expiration);
    buffer.append(s.substring(s.length() - 2, s.length()));
    buffer.append(StringUtils.leftPad(String.valueOf((int) (strike.doubleValue() * 100)), 5, "0"));
    buffer.append(".U");

    return buffer.toString();
}

From source file:com.webbfontaine.valuewebb.model.util.Utils.java

public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int scale) {

    BigDecimal retVal;/*www.  j a  v  a 2 s  .com*/

    if (dividend.doubleValue() == 0) {
        retVal = dividend;
    } else {

        if (divisor == null || divisor.doubleValue() == 0) {
            LOGGER.debug("Division by zero, result will be 'null' !!!");
            retVal = null;
        } else {
            retVal = dividend.divide(divisor, scale, getRoundingMode());
        }
    }

    return retVal;
}

From source file:com.moneychanger.core.helper.bitcoin.BitcoinClient.java

protected static double roundToTwoDecimals(double amount) {
    BigDecimal amountTimes100 = new BigDecimal(amount * 100.0D + 0.5D);
    BigDecimal roundedAmountTimes100 = new BigDecimal(amountTimes100.intValue());
    BigDecimal roundedAmount = roundedAmountTimes100.divide(new BigDecimal(100.0D));

    return roundedAmount.doubleValue();
}

From source file:org.fcrepo.client.test.PerformanceTests.java

private static double round(double d) {
    int decimalPlace = 5;
    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}