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.epam.parso.impl.CSVDataWriterImpl.java

/**
 * The function to convert a double value into a string. If the text presentation of the double is longer
 * than {@link CSVDataWriterImpl#ROUNDING_LENGTH}, the rounded off value of the double includes
 * the {@link CSVDataWriterImpl#ACCURACY} number of digits from the first non-zero value.
 *
 * @param value the input numeric value to convert.
 * @return the string with the text presentation of the input numeric value.
 *///from   w w w  .  j  a va  2 s .  co  m
protected static String convertDoubleElementToString(Double value) {
    String valueToPrint = String.valueOf(value);
    if (valueToPrint.length() > ROUNDING_LENGTH) {
        int lengthBeforeDot = (int) Math.ceil(Math.log10(Math.abs(value)));
        BigDecimal bigDecimal = new BigDecimal(value);
        bigDecimal = bigDecimal.setScale(ACCURACY - lengthBeforeDot, BigDecimal.ROUND_HALF_UP);
        valueToPrint = String.valueOf(bigDecimal.doubleValue());
    }
    valueToPrint = trimZerosFromEnd(valueToPrint);
    return valueToPrint;
}

From source file:Main.java

public static String formatBalance(BigDecimal balance, String curr, boolean round, DecimalFormat format) {
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator(',');
    dfs.setGroupingSeparator(' ');
    DecimalFormat currency = format;
    if (currency == null) {
        if (!round) {
            currency = new DecimalFormat("#,##0.00 ");
        } else {//from   ww w  .  ja v  a  2s  . c o m
            currency = new DecimalFormat("#,##0 ");
        }
    }
    currency.setDecimalFormatSymbols(dfs);
    return currency.format(balance.doubleValue()) + curr;
}

From source file:com.mythesis.userbehaviouranalysis.ProfileAnalysis.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:com.nubits.nubot.utils.Utils.java

public static double round(double value, int places) {
    if (places < 0) {
        throw new IllegalArgumentException();
    }//from  www .j  a v  a 2  s  .co m

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_DOWN);
    return bd.doubleValue();
}

From source file:com.grarak.kerneladiutor.utils.Utils.java

public static double roundTo2Decimals(double val) {
    BigDecimal bd = new BigDecimal(val);
    bd = bd.setScale(2, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

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

/**
 * Converts a list of BigDecimals to a list of Doubles.
 * @param    List<BigDecimal>    list of BigDecimals
 * @return                       list of doubles
 *//*from w ww.j av a2 s .  c om*/
public static List<Double> toDoubleList(List<BigDecimal> list) {

    List<Double> doubleList = new ArrayList<Double>();

    for (BigDecimal val : list) {
        if (!OverFlowUtil.doubleOverflow(val)) {
            OverFlowUtil.overflowError(val);
        }
        doubleList.add(val.doubleValue());
    }

    return doubleList;
}

From source file:com.bjond.utilities.MiscUtils.java

/**
 * Function below courtesy of: /*from   w w  w  . j  ava  2 s  .  c o  m*/
 *    * http://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places 
* 
* 
* @param value value to round.
* @param places places
* @return the rounded double. 
*/
public static double round(final double value, final 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:com.salesmanager.core.util.CurrencyUtil.java

/**
 * Converts a BigDecimal amount from a given currency to another currency
 * // w w  w. java  2  s. c om
 * @param amount
 * @param originCurrency
 * @param toCurrency
 * @return
 */
public static BigDecimal convertToCurrency(BigDecimal amount, String originCurrency, String toCurrency) {

    try {

        // get originCurrency
        Map currencies = RefCache.getCurrenciesListWithCodes();
        double returnAmount = amount.doubleValue();
        com.salesmanager.core.entity.reference.Currency origin = (com.salesmanager.core.entity.reference.Currency) currencies
                .get(originCurrency);
        com.salesmanager.core.entity.reference.Currency convert = (com.salesmanager.core.entity.reference.Currency) currencies
                .get(toCurrency);
        if (origin == null) {
            log.error("Origin currency " + originCurrency + " not found");
            return amount;
        }

        if (convert == null) {
            log.error("Convert currency " + toCurrency + " not found");
            return amount;
        }

        returnAmount = returnAmount / origin.getValue().doubleValue();

        returnAmount = returnAmount * convert.getValue().doubleValue();

        return new BigDecimal(returnAmount).setScale(2, BigDecimal.ROUND_UP);

    } catch (Exception e) {
        log.equals(e);
        return amount;
    }
}

From source file:com.sfs.Formatter.java

/**
 * Round.//from   w ww.  ja va  2  s.c o  m
 *
 * @param value the value
 * @param decimalPlaces the decimal places
 *
 * @return the double
 */
public static double round(final double value, final int decimalPlaces) {
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}

From source file:com.swingtech.commons.util.ClassUtil.java

/**
 * NOTE: When using this to print an object it will not display the
 * primitive type boolean. Must use the wrapper class. All other primitives
 * work fine./*from  ww  w.  j a v a 2s. c om*/
 *
 * NOTE: If an int value has a 0 it won't display.
 *
 * NOTE: Object must have a public constructor.
 *
 * @param object
 * @return
 */
public static String getXMLForObject(final Object object) {
    ByteArrayOutputStream baos = null;
    XMLEncoder e = null;

    baos = new ByteArrayOutputStream();
    e = new XMLEncoder(new BufferedOutputStream(baos));

    e.setPersistenceDelegate(Date.class, new PersistenceDelegate() {
        @Override
        protected Expression instantiate(final Object oldInstance, final Encoder out) {
            final Date date = (Date) oldInstance;
            final Long time = new Long(date.getTime());
            return new Expression(date, date.getClass(), "new", new Object[] { time });
        }
    });

    e.setPersistenceDelegate(BigDecimal.class, new PersistenceDelegate() {
        @Override
        protected Expression instantiate(final Object oldInstance, final Encoder out) {
            final BigDecimal bigDec = (BigDecimal) oldInstance;
            final double doubleVal = bigDec.doubleValue();
            return new Expression(bigDec, bigDec.getClass(), "new", new Object[] { new Double(doubleVal) });
        }
    });

    e.writeObject(object);
    e.close();

    return baos.toString();
}