Java Utililty Methods BigDecimal to

List of utility methods to do BigDecimal to

Description

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

Method

ObjecttoObject(BigDecimal value)
to Object
return value;
BigDecimaltoPercent(final BigDecimal decimalValue, final MathContext mathCntext)
to Percent
return convertNullToZero(decimalValue).divide(ONE_HUNDRED, mathCntext);
StringtoScientificNotation(BigDecimal bd)
Formats a BigDecimal value to a string in scientific notation For example
  • A value of 0.00001234 would be formated as 1.234E-5
  • A value of 100000.00 would be formated as 1.00E5
  • A value of 100 (scale zero) would be formated as 1E2

  • If bd has a precision higher than 20, this method will truncate the output string to have a precision of 20 (no rounding will be done, just a truncate).
    final int truncateAt = 20;
    String unscaled = bd.unscaledValue().toString();
    if (bd.signum() < 0) {
        unscaled = unscaled.substring(1);
    int len = unscaled.length();
    int scale = bd.scale();
    int e = len - scale - 1;
    ...
    
BigDecimaltoSimpleBigDecimal(Object num)
to Simple Big Decimal
try {
    return num != null ? new BigDecimal(num.toString()) : null;
} catch (Exception ignored) {
return null;
StringtoString(BigDecimal num)
to String
if (num == null) {
    return "0";
} else {
    return num.setScale(2).toString();
StringtoString(BigDecimal value, int numberDecimalPlaces)
to String
String newStr = null;
if (value != null) {
    try {
        newStr = String.format("%." + numberDecimalPlaces + "f", value);
        int posCorte = newStr.lastIndexOf(".");
        if (posCorte >= 0) {
            String decimal = newStr.substring(posCorte + 1);
            if (decimal.length() > numberDecimalPlaces) {
...
StringtoString(final BigDecimal dec)
to String
return dec.toPlainString();
StringtoUnsignedString(BigDecimal bigDecimal, int shift)
to Unsigned String
BigDecimal divisor = new BigDecimal(shift);
Deque<Character> numberDeque = new ArrayDeque<Character>();
do {
    BigDecimal[] ba = bigDecimal.divideAndRemainder(divisor);
    bigDecimal = ba[0];
    numberDeque.addFirst(digits[ba[1].intValue()]);
} while (bigDecimal.compareTo(BigDecimal.ZERO) > 0);
StringBuilder builder = new StringBuilder();
...