Example usage for java.math BigDecimal BigDecimal

List of usage examples for java.math BigDecimal BigDecimal

Introduction

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

Prototype

public BigDecimal(long val) 

Source Link

Document

Translates a long into a BigDecimal .

Usage

From source file:Main.java

public static double divide(double v1, double v2, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }//from   www  .  j a v a2s.c  o  m
    BigDecimal b = new BigDecimal(Double.toString(v1));
    BigDecimal one = new BigDecimal(v2);
    return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

/**
 * // ww w  . java  2 s .c o  m
 * <p>
 * newScale scale of the result returned. (1)
 * <p>
 * roundingMode rounding mode to be used to round the result.(ROUND_HALF_UP)
 * 
 * @param value
 * @return BigDecimal
 */
public static BigDecimal convertAccuracy(Float value) {

    final String tmp;
    if (value == null) {
        tmp = new String("0");
    } else {
        tmp = String.valueOf(value);
    }
    return new BigDecimal(tmp).setScale(1, BigDecimal.ROUND_HALF_UP);

}

From source file:Main.java

public static String getNormal(String txamt) {
    String str = txamt;/*from   ww w  . jav  a 2 s. c o  m*/
    if (str != null) {
        try {
            String sum = "";
            int index = 0;
            char c = str.charAt(index);
            while (c == '0') {
                index++;
                c = str.charAt(index);
            }
            sum = str.substring(index);
            double i = Double.parseDouble(sum);
            i = i / 100;
            BigDecimal bg = new BigDecimal(i);
            double j = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            sum = "" + j;
            return sum;

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static Double div(Double v1, Double v2, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }/* w  ww  .j a  v  a2  s  . c o  m*/
    BigDecimal b1 = new BigDecimal(v1.toString());
    BigDecimal b2 = new BigDecimal(v2.toString());
    return Double.valueOf(b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue());
}

From source file:Main.java

public static BigDecimal avg(final List<BigDecimal> list) {
    return list.size() > 0 ? sum(list).setScale(10, RoundingMode.HALF_UP).divide(new BigDecimal(list.size()),
            RoundingMode.HALF_UP) : BigDecimal.ZERO;
}

From source file:Main.java

public static double mul(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.multiply(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static double add(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.add(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static double sub(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.subtract(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static String convertOneDecimal(double price) {
    BigDecimal decimal = new BigDecimal(price);
    return decimal.setScale(1, BigDecimal.ROUND_HALF_UP).toString();
}

From source file:Main.java

public static String convertTwoDecimal(double price) {
    BigDecimal decimal = new BigDecimal(price);
    return decimal.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
}