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

/**
 * Round double value with low error/*from w  w w  .j  a  v  a2s .c  o m*/
 *
 * @param value  Value
 * @param places decimal
 * @return double
 */
static double round(double value, int places) {
    // check if places below 0
    places = Math.max(0, places);
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

public static double sub(float v1, float v2) {
    BigDecimal b1 = new BigDecimal(Float.toString(v1));
    BigDecimal b2 = new BigDecimal(Float.toString(v2));
    return b1.subtract(b2).doubleValue();
}

From source file:Main.java

public static String getFormatSize(double size) {
    double kiloByte = size / 1024;
    if (kiloByte < 1) {
        return size + "B";
    }/*from   w w w.  j a va2  s .c  om*/

    double megaByte = kiloByte / 1024;
    if (megaByte < 1) {
        BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
        return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
    }

    double gigaByte = megaByte / 1024;
    if (gigaByte < 1) {
        BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
        return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
    }

    double teraBytes = gigaByte / 1024;
    if (teraBytes < 1) {
        BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
        return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
    }
    BigDecimal result4 = new BigDecimal(teraBytes);
    return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
}

From source file:Main.java

/**
 * Rounds a double via passed in amount and places
 * @param value//from   w  w  w  .j  av a 2s. c o  m
 * @param places
 * @return
 */
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:Main.java

public static final BigDecimal castToBigDecimal(Object paramObject) {
    if (paramObject == null) {
        return null;
    }/*from   ww  w  .ja  v a 2  s.  com*/
    if ((paramObject instanceof BigDecimal)) {
        return (BigDecimal) paramObject;
    }
    if ((paramObject instanceof BigInteger)) {
        return new BigDecimal((BigInteger) paramObject);
    }
    String str = paramObject.toString();
    if (str.length() == 0) {
        return null;
    }
    return new BigDecimal(str);
}

From source file:Main.java

/**
 * Rounds a double up via passed in amount and places
 * @param value//www  .j a  v  a  2  s . co m
 * @param places
 * @return
 */
public static double roundUp(double value, int places) {
    if (places < 0)
        throw new IllegalArgumentException();

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

From source file:Main.java

/**
 * Rounds a double down via passed in amount and places
 * @param value//www.j  a  v  a2  s  . com
 * @param places
 * @return
 */
public static double roundDown(double value, int places) {
    if (places < 0)
        throw new IllegalArgumentException();

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

From source file:MathUtils.java

public static BigDecimal getBigDecimal(Object value) {
    BigDecimal ret = null;/*w  ww . j  a  va 2  s.  c  om*/
    if (value != null) {
        if (value instanceof BigDecimal) {
            ret = (BigDecimal) value;
        } else if (value instanceof String) {
            ret = new BigDecimal((String) value);
        } else if (value instanceof BigInteger) {
            ret = new BigDecimal((BigInteger) value);
        } else if (value instanceof Number) {
            ret = new BigDecimal(((Number) value).doubleValue());
        } else {
            throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
                    + " into a BigDecimal.");
        }
    }
    return ret;
}

From source file:Main.java

/**
 * Returns a string representation of number rounded to given number of
 * significant figures//  w  ww . j  a v  a2s .  c o  m
 *
 * @param value
 * @param significantFigures
 * @return String.
 */
public static String roundToString(double value, int significantFigures) {
    MathContext mc = new MathContext(significantFigures);
    BigDecimal num = new BigDecimal(value);
    return num.round(mc).toPlainString();
}

From source file:Main.java

public static BigDecimal getNumeric(byte[] ar) throws Exception {
    BigDecimal res = new BigDecimal("0");

    String s = getHexString(ar);//from  www  .ja  v a 2s  .  c o  m
    int len = s.length();
    s = s.substring(0, len - 5) + "." + s.substring(len - 5, len - 1);
    if (ar[ar.length - 1] == 13)
        s = "-" + s;

    res = new BigDecimal(s);
    return res;
}