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

private 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 float 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.floatValue();
}

From source file:Main.java

private static Float precision(int decimalPlace, Float d) {

    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

From source file:Main.java

public static String formatPercent(BigDecimal number) {
    String result = "0";

    if (number != null) {
        number = number.multiply(new BigDecimal("100"));
        DecimalFormat formatter = new DecimalFormat();
        formatter.setMaximumFractionDigits(2);
    }//from ww w  . j  a  va  2 s  . c o m

    return result;
}

From source file:Main.java

public static String formatByte(long l) {
    String s = "0 KB";
    if (l <= 0x100000L) {
        BigDecimal bigdecimal = new BigDecimal(l);
        BigDecimal bigdecimal1 = new BigDecimal(1024);
        RoundingMode roundingmode = RoundingMode.CEILING;
        String s1 = String.valueOf(bigdecimal.divide(bigdecimal1, 0, roundingmode).toString());
        s = (new StringBuilder(s1)).append(" KB").toString();
    } else {//from w  w w .  jav  a2s  .  c  o m
        if (l >= 0x40000000L) {
            BigDecimal bigdecimal2 = new BigDecimal(l);
            BigDecimal bigdecimal3 = new BigDecimal(0x40000000);
            RoundingMode roundingmode1 = RoundingMode.CEILING;
            String s2 = String.valueOf(bigdecimal2.divide(bigdecimal3, 2, roundingmode1).toString());
            s = (new StringBuilder(s2)).append(" G").toString();
        } else {
            BigDecimal bigdecimal4 = new BigDecimal(l);
            BigDecimal bigdecimal5 = new BigDecimal(0x100000);
            RoundingMode roundingmode2 = RoundingMode.CEILING;
            String s3 = String.valueOf(bigdecimal4.divide(bigdecimal5, 2, roundingmode2).toString());
            s = (new StringBuilder(s3)).append(" MB").toString();
        }
    }
    return s;
}

From source file:Main.java

public static final double round(double v, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }/*w  w w .j  av  a  2s.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 div(double v1, double v2, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }/*from w  w w.  ja  va  2  s.c om*/
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static double round(double paramDouble, int paramInt) {
    if (paramInt < 0) {
        throw new IllegalArgumentException("The   scale   must   be   a   positive   integer   or   zero");
    }//  w w w .  java 2  s.c  o m
    return new BigDecimal(Double.toString(paramDouble)).divide(new BigDecimal("1"), paramInt, 5).doubleValue();
}

From source file:Main.java

public static double[] round2(double[] doubleRes) {
    double[] result = new double[doubleRes.length];
    for (int i = 0; i < doubleRes.length; i++) {
        BigDecimal bg = new BigDecimal(doubleRes[i]);
        result[i] = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }//from  w ww  . j  a  v  a  2  s.c  o  m
    return result;
}

From source file:Main.java

protected static final String encodingUTF8andChange(byte[] src, int srart, int length) throws Exception {
    byte[] b = new String(src, srart, length, "euc-kr").getBytes("utf-8");
    String str = new String(b).trim();
    return new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
}