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 formatNumber(int number) {
    double kilo = ((double) number) / 1000;
    if (kilo > 1) {
        BigDecimal result = new BigDecimal(Double.toString(kilo));
        return result.setScale(1, BigDecimal.ROUND_HALF_UP).toPlainString() + "k";
    } else {/*www. j a v a 2s.c om*/
        return number + "";
    }
}

From source file:Main.java

public static double round(double d, int decimalPlace) {
    try {/*from  w  w w.  j  ava 2 s  .  c  o m*/
        BigDecimal bd = new BigDecimal(Double.toString(d));
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    } catch (NumberFormatException e) {
        //on NaN or +/- infinity return the value
        return d;
    }
}

From source file:Main.java

public static String getNormal(String txamt) {
    String str = txamt;//from  ww w .  j a  v  a 2 s. com
    if (str != null) {
        try {
            String sum = "";
            int index = 0;
            char c = str.charAt(index);
            while (c == '0') {
                index++;
                c = str.charAt(index);
            }
            sum = str.substring(index);
            double i = Double.parseDouble(sum);
            i = i / 100;
            BigDecimal bg = new BigDecimal(i);
            double j = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            sum = "" + j;
            return sum;

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static String fileSize2String(long size, int scale) {
    float result;
    String unit;//  w ww  . j  ava  2 s . c om
    if (size <= 0) {
        result = 0;
        unit = "B";
    } else if (size < KB) {
        result = size;
        unit = "B";
    } else if (size < MB) {
        result = size / KB;
        unit = "KB";
    } else if (size < GB) {
        result = size / MB;
        unit = "MB";
    } else {
        result = size / GB;
        unit = "GB";
    }
    BigDecimal bg = new BigDecimal(result);
    float f1 = bg.setScale(scale, BigDecimal.ROUND_HALF_UP).floatValue();
    return f1 + unit;
}

From source file:Main.java

private static String getFormatSize(double size) {
    double kiloByte = size / 1024;
    if (kiloByte < 1) {
        return size + "Byte";
    }/*from  ww  w .  j  a va 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:com.floreantpos.util.NumberUtil.java

public static double roundToTwoDigit(double value) {
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(2, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:com.floreantpos.util.NumberUtil.java

public static double roundToThreeDigit(double value) {
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(3, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

public static String getFormatSize(double size) {
    double kiloByte = size / 1024.0D;
    if (kiloByte < 1.0D) {
        return size + " B"; // Byte(s)
    }/*w  ww .  ja  va  2  s  .  c  om*/

    double megaByte = kiloByte / 1024.0D;
    if (megaByte < 1.0D) {
        BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
        return result1.setScale(2, 4).toPlainString() + " KB";
    }

    double gigaByte = megaByte / 1024.0D;
    if (gigaByte < 1.0D) {
        BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
        return result2.setScale(2, 4).toPlainString() + " MB";
    }

    double teraBytes = gigaByte / 1024.0D;
    if (teraBytes < 1.0D) {
        BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
        return result3.setScale(2, 4).toPlainString() + " GB";
    }
    BigDecimal result4 = new BigDecimal(teraBytes);
    return result4.setScale(2, 4).toPlainString() + " TB";
}

From source file:Main.java

public static String formatFileSize(double size) {
    double kiloByte = size / 1024;
    if (kiloByte < 1) {
        return size + "B";
    }/*  w w  w.ja  va 2  s  .c o  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:Main.java

public static String getFormatSize(double size) {
    double kiloByte = size / 1024;
    if (kiloByte < 1) {
        return size + "Byte(s)";
    }//w w  w . ja v  a  2s  .c  o 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";
}