Example usage for java.math BigDecimal setScale

List of usage examples for java.math BigDecimal setScale

Introduction

In this page you can find the example usage for java.math BigDecimal setScale.

Prototype

@Deprecated(since = "9")
public BigDecimal setScale(int newScale, int roundingMode) 

Source Link

Document

Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal 's unscaled value by the appropriate power of ten to maintain its overall value.

Usage

From source file:org.libreplan.business.reports.dtos.Util.java

public static BigDecimal getFractionalPart(BigDecimal value) {
    if (value == null) {
        return value;
    }/* w w w  . j a  v a 2  s  . c  o  m*/
    BigDecimal fractionalPart = value.subtract(value.setScale(0, RoundingMode.FLOOR));
    return (fractionalPart.compareTo(BigDecimal.ZERO) != 0) ? fractionalPart : null;
}

From source file:Main.java

public static String getMoneyDisplay(BigDecimal amount) {
    if (amount == null) {
        return "0.00";
    } else {/*from  w  w  w  . j a  v a2 s.c om*/
        DecimalFormat df = new DecimalFormat("##.00");
        return df.format(amount.setScale(2, BigDecimal.ROUND_DOWN));
    }
}

From source file:client.Pi.java

/**
 * Compute the value of pi to the specified number of 
 * digits after the decimal point.  The value is 
 * computed using Machin's formula://from  w  w w .  j a  v  a 2  s .  c  o  m
 *
 *          pi/4 = 4*arctan(1/5) - arctan(1/239)
 *
 * and a power series expansion of arctan(x) to 
 * sufficient precision.
 */
public static BigDecimal computePi(int digits) {
    int scale = digits + 5;
    BigDecimal arctan1_5 = arctan(5, scale);
    BigDecimal arctan1_239 = arctan(239, scale);
    BigDecimal pi = arctan1_5.multiply(FOUR).subtract(arctan1_239).multiply(FOUR);
    return pi.setScale(digits, BigDecimal.ROUND_HALF_UP);
}

From source file:Main.java

/**
 * Multiplies two decimals and applies the given scale and a ROUND_HALF_UP. This method should be used only for the final result
 * calculation.For example if we have something like this: (axb)/c the rules should be applied to the result of the division
 * only and not all the computations that give us the final result.
 * //from  w  ww. j a  v a  2s.  co  m
 * @param multiplier1 first multiplier
 * @param multiplier2 second multiplier
 * @param scale the scale fo the result
 * @return the result of the multiplication after scale and rounding are applied
 */
public static BigDecimal multiply(BigDecimal multiplier1, BigDecimal multiplier2, int scale) {

    BigDecimal result = BigDecimal.ZERO;

    if (multiplier1 != null && multiplier2 != null) {
        result = multiplier1.multiply(multiplier2);
    }

    result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
    return result;
}

From source file:Main.java

/**
 * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java
 *//*from  ww  w  .j a v a2 s. 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:com.trenako.entities.Money.java

private static int intValue(BigDecimal d) {
    return d.setScale(2, BigDecimal.ROUND_DOWN).multiply(MONEY_VALUE_FACTOR).intValue();
}

From source file:Main.java

/**
 * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java
 */// w w w  .  j av a  2 s .co  m
public static long parseMoney(String money) {
    String sMoney = money;

    if (sMoney != null) {
        BigDecimal bdMoney;
        sMoney = sMoney.trim(); // to be safe
        try {
            bdMoney = new BigDecimal(sMoney);
            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(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 {
                    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(sMoney);
                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 " + sMoney);
        }
    }
    return 0;
}

From source file:ch.algotrader.util.RoundUtil.java

public static BigDecimal getBigDecimal(double value) {

    if (Double.isNaN(value) || Double.isInfinite(value)) {
        return null;
    } else {/*from   w  w  w .  ja  v a2s.com*/
        BigDecimal decimal = new BigDecimal(value);
        CommonConfig commonConfig = ConfigLocator.instance().getCommonConfig();
        return decimal.setScale(commonConfig.getPortfolioDigits(), BigDecimal.ROUND_HALF_UP);
    }
}

From source file:org.cirdles.ludwig.squid25.Utilities.java

/**
 * Performs excel-style rounding of double to a given number of significant
 * figures./*from  w  ww.  ja va 2s  . c om*/
 *
 * @param value double to round
 * @param sigFigs count of significant digits for rounding
 * @return double rounded to sigFigs significant digits
 */
public static double roundedToSize(double value, int sigFigs) {
    BigDecimal valueBDtoSize = BigDecimal.ZERO;
    if (Double.isFinite(value)) {
        BigDecimal valueBD = new BigDecimal(value);
        int newScale = sigFigs - (valueBD.precision() - valueBD.scale());
        valueBDtoSize = valueBD.setScale(newScale, RoundingMode.HALF_UP);
    }
    return valueBDtoSize.doubleValue();
}

From source file:Main.java

public static String IndianFormat(BigDecimal n) {
    DecimalFormat formatter = new DecimalFormat("#,###.00");
    //we never reach double digit grouping so return
    if (n.doubleValue() < 100000) {
        return formatter.format(n.setScale(2, 1).doubleValue());
    }/*from w w  w .ja  v  a  2 s  .c  o m*/
    StringBuffer returnValue = new StringBuffer();
    //Spliting integer part and decimal part
    String value = n.setScale(2, 1).toString();
    String intpart = value.substring(0, value.indexOf("."));
    String decimalpart = value.substring(value.indexOf("."), value.length());
    //switch to double digit grouping
    formatter.applyPattern("#,##");
    returnValue.append(formatter.format(new BigDecimal(intpart).doubleValue() / 1000)).append(",");
    //appending last 3 digits and decimal part
    returnValue.append(intpart.substring(intpart.length() - 3, intpart.length())).append(decimalpart);
    //returning complete string
    if (returnValue.toString().equals(".00")) {
        return "0.00";
    }

    return returnValue.toString();

}