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 String formatPercentWithFixedFractionDigit(BigDecimal number, int fractionDigit) {
    String result = "0";

    if (number != null) {
        number = number.multiply(new BigDecimal("100"));
        DecimalFormat formatter = new DecimalFormat();
        formatter.setMaximumFractionDigits(fractionDigit);
        formatter.setMinimumFractionDigits(fractionDigit);

        result = formatter.format(number);
    }//ww  w  .  j  av a2  s  . c o m
    return result;
}

From source file:Main.java

public static Object round(Object value, int places) {
    // if (places < 0) throw new IllegalArgumentException();
    if (places < 0 || !(value instanceof Double)) {
        return value;
    }//w w  w. j ava  2 s . c o  m
    BigDecimal bd = new BigDecimal((Double) value);
    bd = bd.setScale(places, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

public static double round(double d, int decimalPlace) {
    // see the Javadoc about why we use a String in the constructor
    // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double)
    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

public static float roundFloat(Float v, int scale) {

    if (scale < 0) {

        throw new IllegalArgumentException(

                "The scale must be a positive integer or zero");

    }/*  w  ww.  j a  v  a 2  s  .  com*/

    BigDecimal b = new BigDecimal(Float.toString(v));

    BigDecimal one = new BigDecimal("1");

    return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).floatValue();

}

From source file:Main.java

public static double roundDouble(double v, int scale) {

    if (scale < 0) {

        throw new IllegalArgumentException(

                "The scale must be a positive integer or zero");

    }//from www  . j  ava  2  s. co m

    BigDecimal b = new BigDecimal(Double.toString(v));

    BigDecimal one = new BigDecimal("1");

    return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();

}

From source file:Main.java

public static double divAddGoods(String value1, String value2) {
    if ("0.".equals(value2)) {
        value2 = "1";
    }/*  ww  w  .  ja  v a 2s .  c  om*/

    double v1 = Double.parseDouble(value1);
    double v2 = Double.parseDouble(value2);
    BigDecimal b1 = new BigDecimal(v1);
    BigDecimal b2 = new BigDecimal((v2));
    return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static BigDecimal calculateDiscountAmt(BigDecimal Discount, BigDecimal DiscountAmt) {
    if (DiscountAmt == null || Discount.compareTo(BigDecimal.ZERO) == 0)
        return BigDecimal.ZERO;
    else {//from   ww w .j av a2 s  .  c om
        BigDecimal discountPerc = Discount.divide(new BigDecimal(100));
        return DiscountAmt.multiply(discountPerc);
    }
}

From source file:Main.java

public static Bitmap compressBitmap(Bitmap bitmap, int width, int height, boolean isAdjust) {
    if (bitmap.getWidth() > width || bitmap.getHeight() > height) {
        float scaleX = new BigDecimal(width).divide(new BigDecimal(bitmap.getWidth()), 4, BigDecimal.ROUND_DOWN)
                .floatValue();//www .ja v a  2 s. c om
        float scaleY = new BigDecimal(height)
                .divide(new BigDecimal(bitmap.getHeight()), 4, BigDecimal.ROUND_DOWN).floatValue();
        if (isAdjust) {
            scaleX = (scaleX < scaleY ? scaleX : scaleY);
            scaleY = scaleX;
        }
        Matrix matrix = new Matrix();
        matrix.postScale(scaleX, scaleY);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
    return bitmap;
}

From source file:Main.java

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

From source file:Main.java

public static String getFormatSize(double size) {
    if (0 == size) {
        return "0K";
    }/*from  ww  w .j a va2s  . co  m*/

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

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

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