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

doublegetScaledDouble(BigDecimal input)
get Scaled Double
double retVal = 0;
if (input != null) {
    retVal = input.setScale(SCALE, ROUNDING_MODE).doubleValue();
return retVal;
BigDecimalgetSid(BigDecimal total, AtomicLong sid)
get Sid
BigDecimal ret = new BigDecimal(sid.getAndIncrement());
if (ret.longValue() > total.longValue()) {
    sid.set(1);
return ret;
StringgetSignedBalance(BigDecimal balance)
Utility method to add a sign to a balance
getSignedBalance(100) returns "+100"
getSignedBalance(-100) returns "-100"
getSignedBalance(0) returns "0"
String signedBalance = "";
if (balance.compareTo(BigDecimal.ZERO) > 0) {
    signedBalance += "+";
signedBalance += balance.setScale(2, RoundingMode.HALF_EVEN).toString();
return signedBalance;
BigDecimalgetTensVal(BigDecimal val)
get Tens Val
return val.remainder(THOUSAND).remainder(HUNDRED);
BigDecimalgetTotalSum(List subTotals)
Sum a list of sub totals to yield a total sum
BigDecimal totalSum = new BigDecimal("0.00");
subTotals.forEach(subTotal -> totalSum.add(subTotal));
return totalSum;
byte[]getUnscaledBytes(BigDecimal bd)
get Unscaled Bytes
if (bd == null) {
    return Arrays.copyOf(NULL_INDICATOR, NULL_INDICATOR.length);
final int scale = bd.scale();
final int precision = bd.precision();
if (scale > 12) {
    throw new IOException("Scale of " + bd + " is " + scale + " and the max is 12");
final int precisionMinusScale = precision - scale;
if (precisionMinusScale > 26) {
    throw new IOException("Precision of " + bd + " to the left of the decimal point is "
            + precisionMinusScale + " and the max is 26");
final int scaleFactor = kDefaultScale - bd.scale();
BigInteger unscaledBI = bd.unscaledValue().multiply(scaleFactors[scaleFactor]);
boolean isNegative = false;
if (unscaledBI.signum() < 0) {
    isNegative = true;
final byte unscaledValue[] = unscaledBI.toByteArray();
if (unscaledValue.length > 16) {
    throw new IOException("Precision of " + bd + " is >38 digits");
return expandToLength16(unscaledValue, isNegative);
StringgetUpdateAccountsQuery(BigDecimal[] result, int id)
get Update Accounts Query
return "UPDATE ACCOUNTS SET SUMB = " + result[0] + "," + "SUMD = " + result[1] + "," + "SUMT = " + result[2]
        + " " + "WHERE Aid = " + id;
StringgetUserLink(BigDecimal id, String name)
Gets the user link.
return "<a href='person.jsp?uid=" + id.toString() + "'>" + name + "</a>";
TgetValue(BigDecimal value, Class clazz)
get Value
return (T) value;
BigDecimalgetValue(String key, Map valuesMap)
get Value
BigDecimal value = BigDecimal.ZERO;
if (valuesMap.containsKey(key)) {
    value = valuesMap.get(key);
return value;