Example usage for java.math BigDecimal valueOf

List of usage examples for java.math BigDecimal valueOf

Introduction

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

Prototype

public static BigDecimal valueOf(double val) 

Source Link

Document

Translates a double into a BigDecimal , using the double 's canonical string representation provided by the Double#toString(double) method.

Usage

From source file:Main.java

public static Double round(Double d) {
    if (d == null) {
        return null;
    }// w ww. j  a  v a2  s.  c  o m
    return BigDecimal.valueOf(d).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

/**
 * // w  w  w.j a va  2 s. c  om
 * @param value
 * @return
 */
public static String expToDecimal(String value) {
    return BigDecimal.valueOf(Double.valueOf(value)).toPlainString();
}

From source file:Main.java

public static float getDecreasePay(int amount, double monthRate, int totalMonth) {
    double v = amount / totalMonth * monthRate;
    return BigDecimal.valueOf(v).setScale(2, RoundingMode.HALF_UP).floatValue();
}

From source file:Main.java

public static String safeDoubleToCurrency(Double val) {
    BigDecimal value = BigDecimal.valueOf(val);
    DecimalFormat kursIndonesia = (DecimalFormat) DecimalFormat.getCurrencyInstance();
    DecimalFormatSymbols formatRp = new DecimalFormatSymbols();

    formatRp.setCurrencySymbol("");
    formatRp.setMonetaryDecimalSeparator('.');
    formatRp.setGroupingSeparator(',');

    kursIndonesia.setDecimalFormatSymbols(formatRp);
    kursIndonesia.setParseBigDecimal(true);
    if (val < 1)
        kursIndonesia.setMaximumFractionDigits(8);
    else if (val < 10)
        kursIndonesia.setMaximumFractionDigits(6);
    else if (val < 100)
        kursIndonesia.setMaximumFractionDigits(4);

    return kursIndonesia.format(value);
}

From source file:Main.java

public static int getFirstMonthPay(int amount, double monthRate, int totalMonth) {
    double firstMonthPay = (amount / totalMonth) + amount * monthRate;
    return BigDecimal.valueOf(firstMonthPay).setScale(2, RoundingMode.HALF_UP).intValue();
}

From source file:Main.java

public static String long2str(long amount) {
    return BigDecimal.valueOf(amount).divide(BigDecimal.valueOf(100L)).toString();
}

From source file:Main.java

public static BigDecimal formatToYuan(String money) throws NumberFormatException {
    if (money.equals("")) {
        return new BigDecimal(0);
    }/* www.  j  a v  a  2s . com*/
    long m = Long.parseLong(money);
    return BigDecimal.valueOf(m);
}

From source file:Main.java

/**
 * /*  w w w  . ja  v  a 2s . com*/
 * @param value
 * @return
 */
public static String expToDecimal(double value) {
    return BigDecimal.valueOf(value).toPlainString();
}

From source file:Main.java

public static Double round(Double d, int scale) {
    if (d == null) {
        return null;
    }//from  ww w  .j a  v  a2  s . com
    return BigDecimal.valueOf(d).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

/**
 * H = -[0.5 * lg(0.5) + 0.4*lg(0.4) + 0.1*lg(0.1)]
 *///from w  w w . j av  a2 s  . co m
public static BigDecimal calculateEntropy(double... probabilities) {
    BigDecimal res = BigDecimal.ZERO;

    for (double singleProbability : probabilities) {

        BigDecimal singleEntropy = BigDecimal.valueOf(singleProbability)
                .multiply(BigDecimal.valueOf(lg2(singleProbability)));

        res = res.add(singleEntropy);
    }

    return res.negate().setScale(ENTROPY_SCALE, BigDecimal.ROUND_HALF_UP);
}