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

floatpercentChange(BigDecimal oldValue, BigDecimal newValue)
percent Change
return divide(newValue.subtract(oldValue), oldValue).multiply(HUNDRED).floatValue();
BigDecimalpercentOf(BigDecimal fullAmount, int percentToKeep)
percent Of
return fullAmount.divide(new BigDecimal("100.00")).multiply(new BigDecimal(percentToKeep)).setScale(2);
BigDecimalpercentToFactor(BigDecimal percent)
percent To Factor
int PERCENT_DECIMAL_SHIFT = 2;
return percent.movePointLeft(PERCENT_DECIMAL_SHIFT);
StringprintBigDecimal(BigDecimal decimal)
print Big Decimal
return String.format("%.2f", decimal);
StringprintGainHTML(BigDecimal gain)
print Gain HTML
String htmlString, arrow;
if (gain.doubleValue() < 0.0) {
    htmlString = "<FONT color=\"#ff0000\">";
    arrow = "arrowdown.gif";
} else {
    htmlString = "<FONT color=\"#009900\">";
    arrow = "arrowup.gif";
htmlString += gain.setScale(SCALE, ROUND) + "</FONT><IMG src=\"images/" + arrow
        + "\" width=\"10\" height=\"10\" border=\"0\"></IMG>";
return htmlString;
intputBigDecimal(byte[] bytes, int offset, BigDecimal val)
Put a BigDecimal value out to the specified byte array position.
if (bytes == null) {
    return offset;
byte[] valueBytes = val.unscaledValue().toByteArray();
byte[] result = new byte[valueBytes.length + SIZEOF_INT];
offset = putInt(result, offset, val.scale());
return putBytes(result, offset, valueBytes, 0, valueBytes.length);
BigDecimalrandomBigDecimal()
random Big Decimal
return BigDecimal.valueOf(randomdouble());
ListrandomBigDecimalList(BigDecimal min, BigDecimal max, int minLength, int maxLength)
Returns a list of randomly generated BigDecimals using the minimum and maximum data point values and the minimum and maximum lengths.
try {
    if (min.compareTo(max) > 0 || minLength >= maxLength) {
        throw new IndexOutOfBoundsException("Index out of bounds generating random big decimal list.");
    List<BigDecimal> list = new ArrayList<BigDecimal>();
    int length = ThreadLocalRandom.current().nextInt(minLength, maxLength + 1);
    for (int i = 0; i < length; i++) {
        BigDecimal random = min.add(new BigDecimal(Math.random()).multiply(max.subtract(min)));
...
BigDecimalrangedValue(BigDecimal lower, BigDecimal upper, Random random)
Generates a new BigDecimal that lies within the specified range.
BigDecimal difference = upper.subtract(lower);
BigDecimal fraction = BigDecimal.valueOf(random.nextDouble());
return lower.add(difference.multiply(fraction));
BigDecimalreadBigDecimal(byte valueBytes[], int valueLength, int scale)
read Big Decimal
int sign = (0 == valueBytes[0]) ? -1 : 1;
byte[] magnitude = new byte[valueLength - 1];
for (int i = 1; i <= magnitude.length; i++)
    magnitude[magnitude.length - i] = valueBytes[i];
return new BigDecimal(new BigInteger(sign, magnitude), scale);