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:json_cmp.Comparer.java

public static void FloatingtoInt(ObjectNode obj, String str) {
    //System.out.println("   " + str + " " + obj.path(str).asDouble());
    BigDecimal temp = new BigDecimal(obj.path(str).asDouble()).setScale(0, BigDecimal.ROUND_HALF_UP);
    //int temp = (int) Math.ceil(obj.path(str).asDouble());
    ((ObjectNode) obj).put(str, temp.toString());
    int output = (int) obj.path(str).asDouble();
    ((ObjectNode) obj).put(str, output);
    //        System.out.println(mediaObj.path(str1).path(str2));
}

From source file:Main.java

/**
 * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java
 *//*w  ww.j  a  v a 2s . c om*/
public static BigDecimal parseMoney(String money) {
    String sMoney = money;

    if (sMoney != null) {
        sMoney = sMoney.trim(); // to be safe
        try {
            return new BigDecimal(sMoney);
        } catch (NumberFormatException e) {
            /* there must be commas, etc in the number.  Need to look for them
             * and remove them first, and then try BigDecimal again.  If that
             * fails, then give up and use NumberFormat and scale it down
             * */
            String[] split = MONEY_PREFIX_PATTERN.split(sMoney);
            if (split.length >= 2) {
                StringBuilder buf = new StringBuilder();
                if (sMoney.startsWith("-")) {
                    buf.append('-');
                }
                for (int i = 0; i < split.length - 1; i++) {
                    buf.append(split[i]);
                }
                buf.append('.');
                buf.append(split[split.length - 1]);
                try {
                    return new BigDecimal(buf.toString());

                } catch (final NumberFormatException e2) {
                    Log.e("QifUtils", "Second parse attempt failed, falling back to rounding");
                }
            }
            NumberFormat formatter = NumberFormat.getNumberInstance();
            try {
                Number num = formatter.parse(sMoney);
                return new BigDecimal(num.floatValue());
            } catch (ParseException ignored) {
            }
            Log.e("QifUtils", "Could not parse money " + sMoney);
        }
    }
    return new BigDecimal(0);
}

From source file:Main.java

public static BigDecimal round(double number, int decimal) {
    return new BigDecimal(number).setScale(decimal, BigDecimal.ROUND_HALF_UP);
}

From source file:Main.java

public static String getFormatSize(double size) {
    double kiloByte = size / 1024;
    if (kiloByte < 1) {
        return size + "Byte";
    }//from   w  w w.  j  ava  2 s  .com

    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

public static double round(double value, int nDecimals) {
    BigDecimal r = new BigDecimal(value);
    return r.setScale(nDecimals, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

/**
 * //from  w ww.j  a va2 s  .c o m
 * @param value
 * @param newScale
 * @param roundingMode
 *            BigDecimal.ROUND_HALF_UP
 * @return
 */
protected static BigDecimal convertAccuracy(Float value, int newScale, int roundingMode) {

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

}

From source file:Main.java

public static double transformTwoDecimalDoubleNumber(double number) {
    BigDecimal bigD = new BigDecimal(number);
    bigD = bigD.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
    return bigD.doubleValue();
}

From source file:Main.java

/**
 * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java
 *///from   w  w  w. ja  va 2s .  c  o m
public static long parseMoney(String money) {
    if (money != null) {
        BigDecimal bdMoney;
        money = money.trim(); // to be safe
        try {
            bdMoney = new BigDecimal(money);
            return moneyAsLong(bdMoney);
        } catch (NumberFormatException e) {
            /* there must be commas, etc in the number.  Need to look for them
             * and remove them first, and then try BigDecimal again.  If that
             * fails, then give up and use NumberFormat and scale it down
             * */
            String[] split = MONEY_PREFIX_PATTERN.split(money);
            if (split.length > 1) {
                StringBuilder buf = new StringBuilder();
                if (money.startsWith("-")) {
                    buf.append('-');
                }
                for (int i = 0; i < split.length - 1; i++) {
                    buf.append(split[i]);
                }
                buf.append('.');
                buf.append(split[split.length - 1]);
                try {
                    bdMoney = new BigDecimal(buf.toString());
                    return moneyAsLong(bdMoney);
                } catch (final NumberFormatException e2) {
                    Log.e("QifUtils", "Second parse attempt failed, falling back to rounding");
                }
            }
            NumberFormat formatter = NumberFormat.getNumberInstance();
            try {
                Number num = formatter.parse(money);
                BigDecimal bd = new BigDecimal(num.floatValue());
                if (bd.scale() > 6) {
                    bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
                }
                return moneyAsLong(bd);
            } catch (ParseException ignored) {
            }
            Log.e("QifUtils", "Could not parse money " + money);
        }
    }
    return 0;
}

From source file:Main.java

@SuppressWarnings("SameParameterValue")
private static double round(double value, int places) {
    if (places < 0) {
        throw new IllegalArgumentException();
    }/*from   ww w.  j a  va  2s .  c o  m*/

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

From source file:Main.java

/**
 * Parses amount strings from GnuCash XML into {@link java.math.BigDecimal}s
 * @param amountString String containing the amount
 * @return BigDecimal with numerical value
 *///  w  w w.  j  a  v a 2  s  .com
public static BigDecimal parseMoney(String amountString) {
    String[] tokens = amountString.split("/");
    BigDecimal numerator = new BigDecimal(tokens[0]);
    BigDecimal denominator = new BigDecimal(tokens[1]);

    return numerator.divide(denominator);
}