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

BigDecimalreadBigDecimal(DataInput in)
Reads BigDecimal value that was written via #writeBigDecimal(BigDecimal,DataOutput) .
byte hint = in.readByte();
switch (hint) {
case DECIMAL_ZERO: {
    return BigDecimal.ZERO;
case DECIMAL_ONE: {
    return BigDecimal.ONE;
case DECIMAL_TEN: {
    return BigDecimal.TEN;
case DECIMAL_SMALL_UNSCALED: {
    long val = readVarLong(in);
    return BigDecimal.valueOf(val);
case DECIMAL_SMALL_SCALED: {
    int scale = readVarIntUnsigned(in);
    long unscaled = readVarLong(in);
    return BigDecimal.valueOf(unscaled, scale);
case DECIMAL_BIG: {
    int scale = readVarIntUnsigned(in);
    int bytesLen = readVarIntUnsigned(in);
    byte[] bytes = new byte[bytesLen];
    in.readFully(bytes);
    return new BigDecimal(new BigInteger(bytes), scale);
default: {
    throw new StreamCorruptedException(
            "Unexpected hint for " + BigDecimal.class.getName() + " value [hint=" + hint + ']');
BigDecimalreadBigDecimal(final Object object)
read Big Decimal
if (object != null) {
    return new BigDecimal((String) object);
} else {
    return null;
BigDecimalreleaseNull(BigDecimal amount)
release Null
return amount == null ? BigDecimal.ZERO : amount;
BigDecimalremoveTrailingZeros(final BigDecimal bd)
remove Trailing Zeros
if (bd.signum() == 0) {
    return bd.setScale(0);
final String text = bd.toPlainString(); 
int scale = bd.scale();
for (int i = text.length() - 1; i >= 0; i--) {
    final char c = text.charAt(i);
    if (c != '0') {
...
intremoveVAT(int priceInCents, BigDecimal vat)
remove VAT
return new BigDecimal(priceInCents).divide(BigDecimal.ONE.add(vat.divide(HUNDRED, 5, UP)), 5, HALF_UP)
        .setScale(0, HALF_UP).intValueExact();
BigDecimalreverseSign(BigDecimal decimal)
reverse +,- sign.
return minus(decimal);
BigDecimalrundeKaufmaennisch(BigDecimal bigDecimal, int stellen)
kaufmaennisch runden.
return bigDecimal == null ? null : bigDecimal.setScale(stellen, BigDecimal.ROUND_HALF_EVEN);
BigDecimalsafeAdd(BigDecimal left, BigDecimal right)
A null-aware method for adding BigDecimal, but only for the right operand.
return right != null ? left.add(right) : left;
BigDecimalsafeAddBD(BigDecimal bd1, BigDecimal bd2)
safe Add BD
if (bd1 == null && bd2 == null) {
    return ZERO;
} else if (bd2 == null) {
    return bd1;
} else if (bd1 == null) {
    return bd2;
} else {
    return bd1.add(bd2);
...
BigDecimalsafeNull(BigDecimal value)
safe Null
return value == null ? BigDecimal.ZERO : value;