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

/**
 * bytes to mb//from w ww .j  a va  2 s  .co m
 * @param bytes
 * @return
 */
public static String bytes2mb(long bytes) {
    BigDecimal filesize = new BigDecimal(bytes);
    BigDecimal megabyte = new BigDecimal(1024 * 1024);
    float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP).floatValue();
    return (returnValue + "  MB ");
}

From source file:Main.java

@SuppressLint("DefaultLocale")
public static String getPriceString(double price) {
    BigDecimal a1 = new BigDecimal(Double.toString(price));
    return String.valueOf(a1.doubleValue());
}

From source file:Main.java

public static String keepNSecimal(String numberStr, int n) {
    double number = Double.parseDouble(numberStr);
    BigDecimal b = new BigDecimal(number);
    String ss = b.setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
    int pointIndex = ss.lastIndexOf(".");
    int fractionCount = ss.length() - pointIndex - 1;
    for (int i = 0; i < n - fractionCount; i++) {
        ss += 0;//ww w .  ja  va  2  s.  co m
    }
    return ss;
}

From source file:Main.java

public static int compareInt(double valone, double valTwo) {
    BigDecimal val1 = new BigDecimal(valone);
    BigDecimal val2 = new BigDecimal(valTwo);
    int result = -1;
    if (val1.compareTo(val2) < 0) {
        result = -1;//from   w ww  .  ja v a2s .  co m
    }
    if (val1.compareTo(val2) == 0) {
        result = 0;
    }
    if (val1.compareTo(val2) > 0) {
        result = 1;
    }
    return result;
}

From source file:Main.java

public static double round(double value, int scale, int roundingMode) {

    BigDecimal bd = new BigDecimal(value);

    bd = bd.setScale(scale, roundingMode);

    double d = bd.doubleValue();

    bd = null;/*w  ww  .ja  va  2 s.c  o  m*/

    return d;

}

From source file:Main.java

public static String doubleParseToString(double d1) {
    Double d = new Double(d1);
    BigDecimal bd = new BigDecimal(d.toString());
    return bd.toPlainString();
}

From source file:Main.java

public static double round(double value, int places) {

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

From source file:Main.java

public static String formatNumber(int number) {
    double kilo = ((double) number) / 1000;
    if (kilo > 1) {
        BigDecimal result = new BigDecimal(Double.toString(kilo));
        return result.setScale(1, BigDecimal.ROUND_HALF_UP).toPlainString() + "k";
    } else {/* ww  w  . j av a 2 s .  c o  m*/
        return number + "";
    }
}

From source file:Main.java

public static Double div(Double v1, Double v2) {
    BigDecimal b1 = new BigDecimal(v1.toString());
    BigDecimal b2 = new BigDecimal(v2.toString());
    return Double.valueOf(b1.divide(b2, DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue());
}

From source file:Main.java

public static BigDecimal formatToYuan(String money) throws NumberFormatException {
    if (money.equals("")) {
        return new BigDecimal(0);
    }//w  ww.  j a v a2s .c om
    long m = Long.parseLong(money);
    return BigDecimal.valueOf(m);
}