Java Utililty Methods BigDecimal

List of utility methods to do BigDecimal

Description

The list of methods to do BigDecimal are organized into topic(s).

Method

BigDecimalsafeToBigDecimal(Object obj1)
safe To Big Decimal
BigDecimal result = BigDecimal.ZERO;
if (obj1 == null) {
    return result;
try {
    result = new BigDecimal(obj1.toString());
} catch (Exception ex) {
return result;
VectorscalarMult(BigDecimal scalar, Vector a)
scalar Mult
Vector<BigDecimal> c = (Vector<BigDecimal>) a.clone();
for (int i = 0; i < a.size(); i++)
    c.set(i, scalar.multiply(((BigDecimal) a.get(i))));
return c;
BigDecimalscale(BigDecimal b1, BigDecimal b2)
scale
return b1.divide(b2, 5).multiply(BigDecimal.valueOf(100));
BigDecimalscale2(BigDecimal valor)
Retorna valor com scale = 2 e RoundingMode.FLOOR
if (null != valor)
    return valor.setScale(2, RoundingMode.FLOOR);
else
    return valor;
BigDecimalscaleCurrency(BigDecimal amount)
scale Currency
return amount.setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimalsecondsBigDecimalFromDuration(long s, int n)
seconds Big Decimal From Duration
if (n == 0)
    return BigDecimal.valueOf(s);
int scale = 9;
boolean huge = (int) s != s;
long ns = huge ? n : s * 1000000000L + n;
while (ns % 10 == 0) {
    ns = ns / 10;
    scale--;
...
intsignum(final BigDecimal value)
If value is null return 0 otherwise the signum().
return value == null ? 0 : value.signum();
BigDecimalsine(BigDecimal x)
sine
BigDecimal lastVal = x.add(BigDecimal.ONE);
BigDecimal currentValue = x;
BigDecimal xSquared = x.multiply(x);
BigDecimal numerator = x;
BigDecimal denominator = BigDecimal.ONE;
int i = 0;
while (lastVal.compareTo(currentValue) != 0) {
    lastVal = currentValue;
...
MapsortByValues(Map map)
sort By Values
List sortedByValueList = new LinkedList(map.entrySet());
Collections.sort(sortedByValueList, new Comparator<Map.Entry<String, BigDecimal>>() {
    public int compare(Entry<String, BigDecimal> o1, Entry<String, BigDecimal> o2) {
        return ((Comparable) (o2).getValue()).compareTo((o1).getValue());
});
Map<String, BigDecimal> sortedHashMap = new LinkedHashMap<String, BigDecimal>();
for (Iterator it = sortedByValueList.iterator(); it.hasNext();) {
...
MapsortMapByBDValue(Map oriMap)
sort Map By BD Value
Map<T, BigDecimal> sortedMap = new LinkedHashMap<T, BigDecimal>();
if (oriMap != null && !oriMap.isEmpty()) {
    List<Map.Entry<T, BigDecimal>> entryList = new ArrayList<Map.Entry<T, BigDecimal>>(oriMap.entrySet());
    Collections.sort(entryList, new Comparator<Map.Entry<T, BigDecimal>>() {
        public int compare(Entry<T, BigDecimal> entry1, Entry<T, BigDecimal> entry2) {
            BigDecimal value1 = entry1.getValue();
            BigDecimal value2 = entry2.getValue();
            return value2.compareTo(value1);
...