Gist of my full code, 31 lines
I'm trying to convert doubles -> BigDecimal then call setScale method on each of the BigDecimal objects stored inside of an array.
// convert double variables to BigDecimal inside of new BigDecimal array
BigDecimal[] ret = new BigDecimal[]{
new BigDecimal(area),
new BigDecimal(theta),
new BigDecimal(x),
new BigDecimal(y)
};
// setScale on each object
for (BigDecimal val: ret) {
val.setScale(2, RoundingMode.FLOOR);
}
But when I print this out, it's clear the setScale function was not called on any of the objects when I print each out later.
Are the objects in the array not being mutated or saved in the 2nd for loop?
Update & Adjustment with Answer-Advice:
Based on Hovercraft's answer I rewrote the last bit of my code here to:
double[] vals = new double[]{area, theta, x, y};
BigDecimal[] ret = new BigDecimal[vals.length];
for (int i = 0; i < vals.length; i++) {
ret[i] = new BigDecimal(vals[i]).setScale(2, RoundingMode.FLOOR);
}
Which does exactly what I wanted.