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:Main.java

public static String getFormatSize(double size) {
    if (0 == size) {
        return "0K";
    }//from  w  ww . j  a  va2 s .com

    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";
}

From source file:Main.java

public static String getFormatSize(double size) {
    double kiloByte = size / 1024;
    if (kiloByte < 1) {
        return size + "B";
    }// www  .  ja v  a 2s. c  om

    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:com.algoTrader.util.RoundUtil.java

public static BigDecimal getBigDecimal(double value) {

    if (!Double.isNaN(value)) {
        BigDecimal decimal = new BigDecimal(value);
        return decimal.setScale(PORTFOLIO_DIGITS, BigDecimal.ROUND_HALF_UP);
    } else {/* w  ww.j a  v a 2s .c o m*/
        return null;
    }
}

From source file:com.algoTrader.util.RoundUtil.java

public static BigDecimal getBigDecimal(double value, int scale) {

    if (!Double.isNaN(value)) {
        BigDecimal decimal = new BigDecimal(value);
        return decimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    } else {//ww w.ja v a2 s.c  om
        return null;
    }
}

From source file:Main.java

public static String getFormatSize(double size) {
    double kiloByte = size / 1024;
    if (kiloByte < 1) {
        return size + "Byte";
    }/*from www  .  j  a v a  2 s  .co  m*/

    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:ch.algotrader.util.RoundUtil.java

public static BigDecimal getBigDecimal(double value, int scale) {

    if (Double.isNaN(value) || Double.isInfinite(value)) {
        return null;
    } else {/*w  w w  .j a v a  2s.  c  om*/
        BigDecimal decimal = new BigDecimal(value);
        return decimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }
}

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

public static BigDecimal getIntegerPart(BigDecimal value) {
    if (value == null) {
        return value;
    }//  www . j  a  v  a  2 s  .c o  m
    return value.setScale(2, RoundingMode.DOWN);
}

From source file:cloudnet.sla.VmSlaOverallDowntime.java

public static double round(double value, int places) {
    if (places < 0) {
        throw new IllegalArgumentException();
    }//from   w w  w  .  ja  v  a2s.c  o m

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

From source file:Main.java

public static double getCpuUsage1() {
    try {//from w w  w.j  a  va  2  s  . c o m
        RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
        String load = reader.readLine();
        String[] toks = load.split(" ");

        double user1 = Double.parseDouble(toks[2]);
        double system1 = Double.parseDouble(toks[4]);
        double irq1 = Double.parseDouble(toks[7]);
        double idle1 = Double.parseDouble(toks[5]);
        try {
            Thread.sleep(360);
        } catch (Exception e) {
            e.printStackTrace();
        }
        reader.seek(0);
        load = reader.readLine();
        reader.close();
        toks = load.split(" ");
        double user2 = Double.parseDouble(toks[2]);
        double system2 = Double.parseDouble(toks[4]);
        double irq2 = Double.parseDouble(toks[7]);
        double idle2 = Double.parseDouble(toks[5]);

        double user_pass = user2 - user1;
        double system_pass = system2 - system1;
        double irq_pass = irq2 - irq1;
        double idle_pass = idle2 - idle1;
        double usage = (user_pass + system_pass + irq_pass) * 100.00
                / (user_pass + irq_pass + system_pass + idle_pass);
        BigDecimal b = new BigDecimal(usage);
        double res = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return res;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return 0;

}

From source file:com.konakart.util.TaxUtils.java

/**
 * Calculates the tax for one or more items
 * //from  w  w w  .  java  2 s . c o m
 * @param taxRate
 *            tax rate as a percentage
 * @param cost
 *            cost of a single item
 * @param quantity
 *            Number of items
 * @param scale
 *            This is the scale used for the precision of the calculations. It is contained in
 *            the ADMIN_CURRENCY_DECIMAL_PLACES configuration variable.
 * @param rule
 *            The rule to be used which should be either TAX_PER_ITEM or TAX_ON_TOTAL.
 *            <ul>
 *            <li>
 *            TaxUtils.TAX_PER_ITEM : The tax is calculated for a single item, to the number of
 *            decimal places defined by scale. Then this value is multiplied by the quantity.
 *            <li>
 *            TaxUtils.TAX_ON_TOTAL : The tax is calculated for the total amount (single item
 *            cost x quantity).
 *            </ul>
 * @return Returns the tax amount
 * 
 */
public static BigDecimal getTaxAmount(BigDecimal taxRate, BigDecimal cost, int quantity, int scale, int rule) {
    if (taxRate == null || cost == null || quantity == 0) {
        return new BigDecimal(0);
    }

    BigDecimal lTaxRate = taxRate.divide(new BigDecimal(100));

    if (rule == TAX_PER_ITEM) {
        BigDecimal taxPerItem = cost.multiply(lTaxRate);
        taxPerItem = taxPerItem.setScale(scale, BigDecimal.ROUND_HALF_UP);
        BigDecimal totalTax = taxPerItem.multiply(new BigDecimal(quantity));
        totalTax = totalTax.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return totalTax;
    } else if (rule == TAX_ON_TOTAL) {
        BigDecimal totalPrice = cost.multiply(new BigDecimal(quantity));
        BigDecimal totalTax = totalPrice.multiply(lTaxRate);
        totalTax = totalTax.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return totalTax;
    }

    // Should never get this far
    return new BigDecimal(0);
}