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

public BigDecimal setScale(int newScale) 

Source Link

Document

Returns a BigDecimal whose scale is the specified value, and whose value is numerically equal to this BigDecimal 's.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123.123456");

    // set scale of bg1 to 6 in bg2
    BigDecimal bg2 = bg1.setScale(6);

    String str = "The value of " + bg1 + " after changing the scale to 6 is " + bg2;

    System.out.println(str);//w  w w . ja va 2  s  .c  om
}

From source file:Main.java

public static String MybigdecimalToString(BigDecimal myBigD) {
    myBigD = myBigD.setScale(2);
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);//  w  ww.  ja v  a2  s .com
    df.setMinimumFractionDigits(0);
    return df.format(myBigD);
}

From source file:NumberUtil.java

/**
 * Returns the BigDecimal value n with trailing
 * zeroes removed.//  ww  w.  j a  v a  2s . com
 */
public static BigDecimal trim(BigDecimal n) {
    try {
        while (true) {
            n = n.setScale(n.scale() - 1);
        }
    } catch (ArithmeticException e) {
        // no more trailing zeroes so exit.
    }
    return n;
}

From source file:com.taobao.adfs.database.tdhsocket.client.util.ConvertUtil.java

public static BigDecimal getBigDecimalFromString(String stringVal, int scale) throws SQLException {
    BigDecimal bdVal;

    if (stringVal != null) {
        if (stringVal.length() == 0) {
            bdVal = new BigDecimal("0");
            try {
                return bdVal.setScale(scale);
            } catch (ArithmeticException ex) {
                try {
                    return bdVal.setScale(scale, BigDecimal.ROUND_HALF_UP);
                } catch (ArithmeticException arEx) {
                    throw new SQLException(
                            "ResultSet.Bad_format_for_BigDecimal: value=" + stringVal + ",scale=" + scale);
                }//from   w ww  . ja va  2 s . c om
            }
        }
        try {
            try {
                return new BigDecimal(stringVal).setScale(scale);
            } catch (ArithmeticException ex) {
                try {
                    return new BigDecimal(stringVal).setScale(scale, BigDecimal.ROUND_HALF_UP);
                } catch (ArithmeticException arEx) {
                    throw new SQLException(
                            "ResultSet.Bad_format_for_BigDecimal: value=" + stringVal + ",scale=" + scale);
                }
            }
        } catch (NumberFormatException ex) {
            throw new SQLException(
                    "ResultSet.Bad_format_for_BigDecimal: value=" + stringVal + ",scale=" + scale);
        }
    }

    return null;
}

From source file:com.owncloud.android.utils.DisplayUtils.java

/**
 * Converts the file size in bytes to human readable output.
 * <ul>/* ww w . j  av a2  s.com*/
 *     <li>appends a size suffix, e.g. B, KB, MB etc.</li>
 *     <li>rounds the size based on the suffix to 0,1 or 2 decimals</li>
 * </ul>
 *
 * @param bytes Input file size
 * @return Like something readable like "12 MB"
 */
public static String bytesToHumanReadable(long bytes, Context context) {
    if (bytes < 0) {
        return context.getString(R.string.common_pending);

    } else {
        double result = bytes;
        int attachedSuff = 0;
        while (result >= 1024 && attachedSuff < sizeSuffixes.length) {
            result /= 1024.;
            attachedSuff++;
        }

        BigDecimal readableResult = new BigDecimal(result)
                .setScale(sizeScales[attachedSuff], BigDecimal.ROUND_HALF_UP).stripTrailingZeros();

        // Unscale only values with ten exponent
        return (readableResult.scale() < 0 ? readableResult.setScale(0) : readableResult) + " "
                + sizeSuffixes[attachedSuff];
    }
}

From source file:dk.clanie.bitcoin.AddressAndAmount.java

public AddressAndAmount(String address, BigDecimal amount) {
    this.address = address;
    this.amount = amount.setScale(BitcoindClient.SCALE);
}

From source file:fakingXmocking.CurrencyConversionHttpClientFake.java

private String formatResultContainingCurrencyConversion(String[] params) {
    String from = getParameter(params, "from");
    String to = getParameter(params, "to");
    BigDecimal rate = findConversionRate(from, to);

    currenciesAndRates.put(from + '>' + to, rate.setScale(2));
    currenciesAndRates.put(to + '>' + from, BigDecimal.ONE.divide(rate, 2, RoundingMode.HALF_UP));

    return "<div id=\"converter_results\"><ul><li><b>1 " + from + " = " + rate + ' ' + to + "</b>";
}

From source file:hjow.hgtable.util.DataUtil.java

/**
 * <p>2? 1  ? .</p>//from   w  w w. j a  v a2  s  .com
 * 
 * @param original : ?? 
 * @param scale :  ?
 * @return 2? 1 
 */
public static BigDecimal sqrt(BigDecimal original, int scale) {
    BigDecimal temp = new BigDecimal(String.valueOf(original));

    BigDecimal results = new BigDecimal("1.0");
    results.setScale(scale + 2);

    int loops = 0;

    while (true) {
        if (loops >= 1) {
            temp = new BigDecimal(String.valueOf(results));
        }

        temp.setScale(scale + 2, BigDecimal.ROUND_FLOOR);
        results = original.divide(temp, scale + 2, BigDecimal.ROUND_FLOOR).add(temp)
                .divide(new BigDecimal("2.0"), scale + 2, BigDecimal.ROUND_FLOOR);
        if (temp.equals(results))
            break;

        loops++;
    }

    return results.setScale(scale, BigDecimal.ROUND_HALF_UP);
}

From source file:eu.bittrade.libs.steemj.protocol.Asset.java

/**
 * Transform this asset into its {@link BigDecimal} representation.
 * /*from  www.j a  v  a  2 s .  c o m*/
 * @return The value of this asset in its {@link BigDecimal} representation.
 */
public BigDecimal toReal() {
    BigDecimal transformedValue = new BigDecimal(this.getAmount());
    return transformedValue.setScale(this.getPrecision());
}

From source file:org.libreplan.business.templates.entities.OrderLineTemplate.java

public void setBudget(BigDecimal budget) {
    Validate.isTrue(budget.compareTo(BigDecimal.ZERO) >= 0, "budget cannot be negative");
    this.budget = budget.setScale(2);
}