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:com.mycompany.mavenproject1.robertclasses.CheckFormat.java

public static boolean isBigDecimal(String str) {
    try {/*w w  w . j  a va2s.c  o  m*/
        BigDecimal bigdecimal = new BigDecimal(str);
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static float formatFloat(float floatValue, int newScale) {
    BigDecimal b = new BigDecimal(floatValue);
    return b.setScale(newScale, BigDecimal.ROUND_HALF_UP).floatValue();
}

From source file:Main.java

public static BigDecimal getRank(long date) {
    String time = (date / 1000) + ".00";
    return new BigDecimal(time).negate();
}

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  ww .ja va 2s .  com*/
    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();

}

From source file:Main.java

public static BigDecimal parseBalance(String balance) {
    if (balance == null) {
        return new BigDecimal(0);
    }//from  w  w  w  .  j  a v  a2s  .c  om
    balance = balance.replaceAll("[^0-9,.-]*", "");
    balance = balance.replace(",", ".");
    if (balance.indexOf(".") != balance.lastIndexOf(".")) {
        String b = balance.substring(balance.lastIndexOf("."));
        balance = balance.substring(0, balance.lastIndexOf("."));
        balance = balance.replace(".", "");
        balance = balance + b;
    }
    BigDecimal ret;
    try {
        ret = new BigDecimal(balance);
    } catch (NumberFormatException e) {
        Log.e("parseBalance", "Unable to parse: " + balance);
        ret = new BigDecimal(0);
    }
    return ret;
}

From source file:Main.java

public static double round(double d, int decimalPlace) {
    try {//from  ww  w  . j av a 2s .  c  om
        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 fileSize2String(long size, int scale) {
    float result;
    String unit;/*from w w  w. jav a  2s  .c  o  m*/
    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

/**
 * Checks whether the given string can be converted to a decimal.
 * //from   w w w. j a  v  a 2s . co m
 * @param s the string to check
 * @return true, iff the sting can be converted to a decimal
 */
private static boolean isDecimal(String s) {
    try {
        new BigDecimal(s);
        return true;
    } catch (NumberFormatException nfe) {
        return false;
    }
}

From source file:Main.java

static BigDecimal bigDecimalElement(Element row, String name) {
    return new BigDecimal(stringElement(row, name));
}

From source file:Main.java

public static long toSatoshi(String amount) {
    return new BigDecimal(amount).movePointRight(8).toBigIntegerExact().longValue();
}